Shake visual effect on iPhone (NOT shaking the device)

后端 未结 11 826
忘了有多久
忘了有多久 2020-12-02 05:06

On login failure, I\'d prefer to avoid showing an alert, it\'s too fleeting. Showing the alert and then showing the text somewhere on the login screen seems like duplication

11条回答
  •  执念已碎
    2020-12-02 05:33

    I know the question is already answered, but since I have already implemented something like this previously, I feel it can't 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"];
    

    Also, since you want the shaking to indicate that the user failed to log in, you might also consider adding this animation that tints the screen red while the screen shakes:

    //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!

提交回复
热议问题