2011-08-11 41 views
7

Tôi đang cố tạo biểu tượng "lắc".Hoạt ảnh "Wobbly" trên iPhone UiImageView

On tải điều khiển của tôi, tôi tạo ra một Timer như thế này:

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(shakeIphonePic) userInfo:nil repeats:YES]; 

Và đây là phương pháp shaker tôi:

- (void)shakeIphonePic 
{ 
    [UIView animateWithDuration:0.09 
          delay:0 
         options:UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         self.iphonePic.layer.transform = CATransform3DMakeRotation(DegreesToRadians(8.0), 0.0, 0.0, 1.0); 
        } 
        completion:^(BOOL finished) { 
         [UIView animateWithDuration:0.09 
             animations:^(void) { 
              self.iphonePic.layer.transform = CATransform3DMakeRotation(DegreesToRadians(-16.0), 0.0, 0.0, 1.0); 
             }]; 
        } 
    ]; 
} 

Nó không phải là tốt đẹp như tôi mong đợi nhưng ... đây không phải là vấn đề chính.

Dường như nó làm chậm đáng kể phần còn lại của giao diện người dùng của tôi, mà trước đây là tốt.

Bạn có thể gợi ý cho tôi cách lắc biểu tượng hiệu quả hơn không?

Trả lời

27
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
[anim setToValue:[NSNumber numberWithFloat:0.0f]]; 
[anim setFromValue:[NSNumber numberWithDouble:M_PI/16]]; // rotation angle 
[anim setDuration:0.1]; 
[anim setRepeatCount:NSUIntegerMax]; 
[anim setAutoreverses:YES]; 
[self.viewYouAreShaking.layer addAnimation:anim forKey:@"iconShake"]; 

Swift phiên bản

let anim=CABasicAnimation(keyPath: "transform.rotation") 
anim.toValue=NSNumber(double: -M_PI/16) 
anim.fromValue=NSNumber(double: M_PI/16) 
anim.duration=0.1 
anim.repeatCount=1.5 
anim.autoreverses=true 
viewYouAreShaking.layer.addAnimation(anim, forKey: "iconShake") 
+1

Một giới thiệu tốt là chương Animation của Lập trình iOS4 Matt Neuburg. Bản nháp công khai có sẵn tại http://www.apeth.com/iOSBook/ch17.html – Jano

+0

Làm cách nào để tắt hoạt ảnh? Tôi đã thử '[self.viewYouAreShaking.layer removeAllAnimations];' nhưng rung lắc lại sau khi bạn nhấc ngón tay lên. Làm thế nào tôi có thể làm cho nó bỏ qua các liên lạc báo chí dài lên và chỉ nhận ra các liên lạc báo chí dài xuống? – Mason

Các vấn đề liên quan