2011-08-11 13 views
7

私はアイコンを "揺らす"ようにしようとしています。私のコントローラをロードするにはiPhone UiImageViewの "Wobbly"アニメーション

私はこのようなタイマーを作成します。それは私が期待ほど素敵ではないですが...これではありません

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

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

そして、ここでは私のシェーカー方法です主な問題。

これまでのUIのそれは劇的に遅くなっているようです。

あなたは私のアイコンを振るより効率的な方法を提案できますか?

答えて

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"]; 

スウィフトバージョン

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

良い導入がマット・ニューバーグによるプログラミングiOS4をのアニメーションの章です。公開草案はhttp://www.apeth.com/iOSBook/ch17.htmlから入手できます – Jano

+0

アニメーションをどのようにオフにしますか?私は '[self.viewYouAreShaking.layer removeAllAnimations];を試しましたが、あなたの指を持ち上げた後には揺れが止まります。長押しのタッチを無視し、長押しのタッチを認識するにはどうすればよいですか? – Mason

関連する問題