UiImageView上的iPhone“Wobbly”动画

时间:2011-08-11 09:53:54

标签: iphone ios uiview core-animation

我试图让图标“颤抖”。

在加载我的控制器时,我创建了一个这样的Timer:

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

这是我的摇床方法:

- (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);
                                         }];
                    }
    ];
}

它不如我预期的那么好但是......这不是主要问题。

看起来它显着减慢了我的UI的其余部分,之前很好。

你能否建议我更有效地动摇我的偶像?

1 个答案:

答案 0 :(得分: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版本

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")