I know that the question has already been answered, but since I already implemented something similar earlier, I feel that this will not hurt to add it:
CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"]; NSArray *transformValues = [NSArray arrayWithObjects: [NSNumber numberWithFloat:((M_PI)/64)], [NSNumber numberWithFloat:(-((M_PI)/64))], [NSNumber numberWithFloat:((M_PI)/64)], [NSNumber numberWithFloat:(-((M_PI)/64))], [NSNumber numberWithFloat:((M_PI)/64)], [NSNumber numberWithFloat:(-((M_PI)/64))], [NSNumber numberWithFloat:0], nil]; [shakeAnimation setValues:transformValues]; NSArray *times = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.14f], [NSNumber numberWithFloat:0.28f], [NSNumber numberWithFloat:0.42f], [NSNumber numberWithFloat:0.57f], [NSNumber numberWithFloat:0.71f], [NSNumber numberWithFloat:0.85f], [NSNumber numberWithFloat:1.0f], nil]; [shakeAnimation setKeyTimes:times]; shakeAnimation.fillMode = kCAFillModeForwards; shakeAnimation.removedOnCompletion = NO; shakeAnimation.duration = 0.6f; [self.viewToShake.layer addAnimation:shakeAnimation forKey:@"anim"];
In addition, since you want the shaking to indicate that the user was unable to log in, you can also think of adding this animation, which shades the red screen while the screen is shaking:
//Put this in the header (.h) @property (nonatomic, strong) UIView *redView; //Put this in the implementation (.m) @synthesize redView; //Put this in viewDidLoad self.redView = [[UIView alloc] initWithFrame:self.view.frame]; self.redView.layer.opacity = 0.0f; self.redView.layer.backgroundColor = [[UIColor redColor] CGColor]; //Put this wherever you check if the login failed CAKeyframeAnimation *redTint = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; NSArray *transformValues = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.2f], [NSNumber numberWithFloat:0.0f], nil]; [redTint setValues:transformValues]; NSArray *times = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.5f], [NSNumber numberWithFloat:1.0f], nil]; [redTint setKeyTimes:times]; redTint.fillMode = kCAFillModeForwards; redTint.removedOnCompletion = NO; redTint.duration = 0.6f; [self.redView.layer addAnimation:shakeAnimation forKey:@"anim"];
Hope this helps!
pasawaya Jun 20 2018-12-12T00: 00Z
source share