2009-11-11 13 views
6

私は、あたかも打たれているかのように、UI要素、つまりコントロールを振動/揺れようとしています。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)を中心に前後に小さなアークによって回転することができる:私は1つの画素、水平方向またはその両方、垂直に振るコアアニメーションを使用することができ:

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

これらのどれも気に入らない。誰かに良い選択肢がありますか?私は特に、試してみるためにcoolPathのアイデアを感謝します。

ありがとうございます!

アップデート:私は契約および制御を拡大しています

以下は、より良いですが、私はまだ他のアイデアを探しています。

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

私が使用して間違っているユーザーの入力した後、私の入力・ビューの1を振っています:

- (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

レイヤーの位置にキーフレームアニメーションを使用できます。これは、私があなたが「気に入っている」と考えているか分からないので、もちろん主観的です。 OS Xのパスワードを間違って入力したときに発生するシェイクアニメーションは、このblog post on Cocoa Is My GirlfriendでCore Animationを使用して複製されました。それがあなたのために起こっているのかどうかは分かりませんが、これは一つの選択肢です。

敬具、

+0

これが唯一であります一方向 - 左右に - 右? – mahboudz

+0

はい。しかし、CGPointである 'position'フィールドをアニメートするのと同じ方法でパスを使用するキーフレームアニメーションを作成することができます。 –

2

ここBersaelor'sちょっといい手ブレ答えの更新代わりにUIViewブロックのアニメーションを使用するには、いくつかの人々はいつもあなたがオートリバースを使用して、これらの中で、カウントを繰り返すことができます知っていない:

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; 
       }]; 
関連する問題