2011-09-21 13 views
7

मैं वास्तव में अपने आईओएस एप्लिकेशन में एक UILabel एनिमेट करने के साथ एक समस्या पर अटक गया। कोड स्निपेट के लिए वेब खोजने के 2 दिनों के बाद, अभी भी कोई परिणाम नहीं है।आईओएस ऐप में CoreAnimation/QuartzCore के साथ UILabel एनिमेटिंग

मैंने पाया कि प्रत्येक नमूना UIImage को एनिमेट करने के तरीके के बारे में था, इसे परत द्वारा UIView के उप-दृश्य के रूप में जोड़ना था। UILabel एनिमेट करने के बारे में कोई अच्छा उदाहरण है? मैं इस तरह अल्फा संपत्ति की स्थापना, द्वारा एक निमिष एनीमेशन के लिए एक अच्छा समाधान नहीं मिला:

मेरे समारोह:

- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target 
{ 
    NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:@"EffectSpeed"]; 
    float speedFloat = (1.00 - [selectedSpeed floatValue]); 

    [UIView beginAnimations:animationID context:target]; 
    [UIView setAnimationDuration:speedFloat]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)]; 

    if([target alpha] == 1.0f) 
     [target setAlpha:0.0f]; 
    else 
     [target setAlpha:1.0f]; 
    [UIView commitAnimations]; 
} 

UILabel पर मेरे समारोह कॉल करें:

[self blinkAnimation:@"blinkAnimation" finished:YES target:labelView]; 

लेकिन कैसे के बारे में एक पल्स, या स्केलिंग एनीमेशन?

उत्तर

13

दुर्भाग्य से फ़ॉन्ट आकार NSView की एक एनिमेटेबल संपत्ति नहीं है। आदेश में एक UILabel पैमाने पर करने के लिए, आपको CAKeyframeAnimation का उपयोग कर, और अधिक उन्नत Core Animation तकनीक का उपयोग करने की आवश्यकता होगी:

  1. आयात अपने प्रोजेक्ट में QuartzCore.framework, और #import <QuartzCore/QuartzCore.h> अपने कोड में।
  2. नया CAKeyframeAnimation ऑब्जेक्ट बनाएं जिससे आप अपने मुख्य फ्रेम जोड़ सकें।
  3. स्केलिंग ऑपरेशन को परिभाषित करने वाला CATransform3D मान बनाएं (3 डी भाग द्वारा भ्रमित न हों - आप किसी भी परत पर परिवर्तन करने के लिए इस ऑब्जेक्ट का उपयोग करते हैं)।
  4. एनीमेशन में CAKeyframeAnimation ऑब्जेक्ट को setValues विधि का उपयोग करके एनीमेशन में कीफ्रेम में से एक को परिवर्तन करें।

    // Create the keyframe animation object 
    CAKeyframeAnimation *scaleAnimation = 
        [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
    
    // Set the animation's delegate to self so that we can add callbacks if we want 
    scaleAnimation.delegate = self; 
    
    // Create the transform; we'll scale x and y by 1.5, leaving z alone 
    // since this is a 2D animation. 
    CATransform3D transform = CATransform3DMakeScale(1.5, 1.5, 1); // Scale in x and y 
    
    // Add the keyframes. Note we have to start and end with CATransformIdentity, 
    // so that the label starts from and returns to its non-transformed state. 
    [scaleAnimation setValues:[NSArray arrayWithObjects: 
            [NSValue valueWithCATransform3D:CATransform3DIdentity], 
            [NSValue valueWithCATransform3D:transform], 
            [NSValue valueWithCATransform3D:CATransform3DIdentity], 
            nil]]; 
    
    // set the duration of the animation 
    [scaleAnimation setDuration: .5]; 
    
    // animate your label layer = rock and roll! 
    [[self.label layer] addAnimation:scaleAnimation forKey:@"scaleText"]; 
    
    :
  5. अपने setDuration विधि
  6. अंत में फोन करके एनीमेशन के लिए अवधि सेट करें, लेबल की परत का उपयोग [[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"]

अंतिम कोड कुछ इस तरह दिखाई सकता है एनीमेशन जोड़ने

मैं आपके लिए एक अभ्यास के रूप में दोहराव "नाड़ी" एनीमेशन छोड़ दूंगा: संकेत, इसमें animationDidStop विधि शामिल है!

एक अन्य नोट - कैलियर एनिमेटेबल गुणों की पूरी सूची (जिसमें से "ट्रांसफॉर्म" एक है) here पाया जा सकता है। हैप्पी ट्विनिंग!

+0

आपके विस्तृत उत्तर के लिए बहुत बहुत धन्यवाद। टाइमर के साथ CABasicAnimation का उपयोग करके, मैंने समस्या को हल कर लिया है। मुझे यकीन है कि आपका कोड भी ठीक काम करेगा, इसलिए आप स्वीकार करते हैं। ;) – DevZarak

संबंधित मुद्दे