2009-11-11 12 views
6

मैं एक UI तत्व को हिलाने/हिलाकर रखने की कोशिश कर रहा हूं, अर्थात् एक नियंत्रण, जैसे कि यह मारा गया था, और गूंज रहा है। मैं इसे खड़ी हिला, क्षैतिज कोर एनिमेशन का उपयोग करें या दोनों, एक पिक्सेल द्वारा कर सकते हैं:यूआई आइटम को कंपन करने का सबसे अच्छा तरीका?

CABasicAnimation* rotationXAnimation; 
rotationXAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; 
rotationXAnimation.fromValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y-1.0 ]; 
rotationXAnimation.toValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y+1.0 ]; 
rotationXAnimation.duration = 0.2; 
rotationXAnimation.cumulative = NO; 
rotationXAnimation.repeatCount = 10.0; 
rotationXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationXAnimation forKey:@"upDownAnimation"]; 

या मैं आगे और पीछे z अक्ष (-M_PI * 0.02 और M_PI * 0.02) के चारों तरफ छोटे चाप से घुमा सकते हैं :

CABasicAnimation* rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat: -M_PI * 0.02 ]; 
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 0.02 ]; 
rotationAnimation.duration = 0.2; 
rotationAnimation.cumulative = NO; 
rotationAnimation.repeatCount = 10.0; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

इनमें से कोई भी आकर्षक नहीं है। क्या किसी के पास कुछ अच्छे विकल्प हैं? मैं विशेष रूप से कोशिश करने के लिए शांत keyPaths विचारों की सराहना करता हूं।

धन्यवाद!

अद्यतन:

निम्नलिखित, मैं कहाँ करार कर रहा हूँ और नियंत्रण का विस्तार, बेहतर है, लेकिन मैं अभी भी अन्य विचारों की तलाश कर रहा हूँ।

CABasicAnimation* scaleAnimation; 
scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
scaleAnimation.fromValue = [NSNumber numberWithFloat: 0.95 ]; 
scaleAnimation.toValue = [NSNumber numberWithFloat: +1.05 ]; 
scaleAnimation.duration = 0.1; 
scaleAnimation.cumulative = NO; 
scaleAnimation.repeatCount = 10.0; 
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:scaleAnimation forKey:@"scaleAnimation"]; 

उत्तर

6

मैं की मेरी इनपुट-दृश्य एक मिलाते हुए कर रहा हूँ एक गलत उपयोगकर्ता इनपुट के बाद का उपयोग कर:

- (void)earthquake:(UIView*)itemView 
{ 
    CGFloat t = 2.0; 
    CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t); 
    CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t); 

    itemView.transform = leftQuake; // starting point 

    [UIView beginAnimations:@"earthquake" context:itemView]; 
    [UIView setAnimationRepeatAutoreverses:YES]; // important 
    [UIView setAnimationRepeatCount:4]; 
    [UIView setAnimationDuration:0.07]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)]; 

    itemView.transform = rightQuake; // end here & auto-reverse 

    [UIView commitAnimations]; 
} 

- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([finished boolValue]) 
    { 
     UIView* item = (UIView *)context; 
     item.transform = CGAffineTransformIdentity; 
    } 
} 

यह काफी स्वाभाविक लग रहा है, मैं कुछ इंटरनेट-धागे से इस कोड को मिल गया है कि मुझे याद नहीं है

4

आप परत स्थिति पर एक कीफ्रेम एनीमेशन का उपयोग कर सकते हैं। यह निश्चित रूप से व्यक्तिपरक है क्योंकि मुझे यकीन नहीं है कि आप "प्रसन्न" पर विचार करते हैं। जब आप अपना ओएस एक्स पासवर्ड दर्ज करते हैं तो आपको हिलाए गए शेक एनीमेशन को इस blog post on Cocoa Is My Girlfriend में कोर एनीमेशन का उपयोग करके डुप्लिकेट किया गया है। सुनिश्चित नहीं है कि आप यही चाहते हैं, लेकिन यह एक विकल्प है।

सादर,

+0

यह केवल में है एक दिशा - साइड-टू-साइड - सही? – mahboudz

+0

हां। लेकिन आप एक कीफ्रेम एनीमेशन बना सकते हैं जो एक पथ का उपयोग उसी स्थिति का उपयोग करता है जो 'स्थिति' फ़ील्ड को एनिमेट करता है जो एक CGPoint है। –

2

बजाय UIView ब्लॉक एनिमेशन का उपयोग करने के लिए, कुछ लोगों को हमेशा आपको जानने वाले ऑटो-रिवर्स उपयोग कर सकते हैं नहीं है और दोहराने-मायने रखता है इन में Bersaelor's अच्छी छोटी शेक जवाब यहाँ की एक अद्यतन:

self.transform = CGAffineTransformMakeTranslation(2.0, -2.0); 
[UIView animateWithDuration:0.07 
         delay:0.0 
        options:UIViewAnimationOptionAutoreverse 
       animations:^{ 
        UIView.animationRepeatCount = 4; 
        self.transform = CGAffineTransformMakeTranslation(-2.0, 2.0); 
       } 
       completion:^(BOOL finished){ 
        self.transform = CGAffineTransformIdentity; 
       }]; 
संबंधित मुद्दे