"Wobbly" iPhone Animation on UiImageView

I am trying to make the icon trembling.

When loading my controller, I create a timer as follows:

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

And here is 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); }]; } ]; } 

This is not as good as I expected, but ... this is not the main problem.

It seems like this significantly slows down the rest of my user interface, which used to be good.

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

+7
source share
1 answer
 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"]; 

Quick 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") 
+27
source

All Articles