Shake visual effect on iPhone (NOT shaking the device)

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

    Using iOS 4+ block based UIKit animations (and loosely based on on jayccrown's answer):

    - (void)shakeView:(UIView *)viewToShake
    {
        CGFloat t = 2.0;
        CGAffineTransform translateRight  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
        CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
    
        viewToShake.transform = translateLeft;
    
        [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
            [UIView setAnimationRepeatCount:2.0];
            viewToShake.transform = translateRight;
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                    viewToShake.transform = CGAffineTransformIdentity;
                } completion:NULL];
            }
        }];
    }
    

提交回复
热议问题