UIView shake animation

后端 未结 16 1292
梦毁少年i
梦毁少年i 2020-11-28 18:07

i\'m trying to make a UIView shake when a button is pressed.

I am adapting the code I found on http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-e

16条回答
  •  情歌与酒
    2020-11-28 18:54

    Here is a version using,

    + (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
    

    Introduced in iOS 7.

        const CGFloat xDelta = 16.0f;
    
        [UIView animateKeyframesWithDuration:0.50f
                                       delay:0.0f
                                     options:UIViewKeyframeAnimationOptionCalculationModeLinear
                                  animations:^{
    
                                      [UIView addKeyframeWithRelativeStartTime:0.0
                                                              relativeDuration:(1.0/6.0)
                                                                    animations:^{
                                                                        self.passwordTextField.transform = self.usernameTextField.transform = CGAffineTransformMakeTranslation(xDelta, 0.0);
                                                                    }];
    
                                      [UIView addKeyframeWithRelativeStartTime:(1.0/6.0)
                                                              relativeDuration:(1.0/6.0)
                                                                    animations:^{
                                                                        self.passwordTextField.transform = self.usernameTextField.transform = CGAffineTransformMakeTranslation(-xDelta, 0.0);
                                                                    }];
    
                                      [UIView addKeyframeWithRelativeStartTime:(1.0/3.0)
                                                              relativeDuration:(1.0/3.0)
                                                                    animations:^{
                                                                        self.passwordTextField.transform = self.usernameTextField.transform = CGAffineTransformMakeTranslation(xDelta/2.0, 0.0);
                                                                    }];
    
                                      [UIView addKeyframeWithRelativeStartTime:(2.0/3.0)
                                                              relativeDuration:(1.0/3.0)
                                                                    animations:^{
                                                                        self.passwordTextField.transform = self.usernameTextField.transform = CGAffineTransformIdentity;
                                                                    }];
    
                                  }
                                  completion:NULL];
    

提交回复
热议问题