Shake visual effect on iPhone (NOT shaking the device)

后端 未结 11 793
忘了有多久
忘了有多久 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:30

    In iOS 7.0 or later, UIKit keyframe animation is available.

    [UIView animateKeyframesWithDuration:0.5 delay:0.0 options:0 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    
        NSInteger repeatCount = 8;
        NSTimeInterval duration = 1.0 / (NSTimeInterval)repeatCount;
    
        for (NSInteger i = 0; i < repeatCount; i++) {
            [UIView addKeyframeWithRelativeStartTime:i * duration relativeDuration:duration animations:^{
                CGFloat dx = 5.0;
                if (i == repeatCount - 1) {
                    viewToShake.transform = CGAffineTransformIdentity;
                } else if (i % 2) {
                    viewToShake.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, -dx, 0.0);
                } else {
                    viewToShake.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, +dx, 0.0);
                }
            }];
        }
    } completion:completion];
    

提交回复
热议问题