Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone "Wobbly" animation on UiImageView

I'm trying to make an icon "shaking".

On loading my controller I create a Timer like this:

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

And here's my shaker method:

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

It's not as nice as I expected but... this is not the main problem.

It looks like it dramatically slow down the rest of my UI, which before was good.

Can you suggest me a more efficient way to shake my icon?

like image 807
Fabio B. Avatar asked Aug 11 '11 09:08

Fabio B.


1 Answers

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 version

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")
like image 114
Jano Avatar answered Nov 07 '22 22:11

Jano