How to make a view vibrate in an animation in obje

2020-03-31 05:11发布

I have a UIView called qShake. When the user makes an error, I want that view to shake from left to right in a small space for a short period of time. How would I create an animation to do that?

1条回答
Bombasti
2楼-- · 2020-03-31 06:10

You can use a CABasicAnimation, and set the repeat count to some small value, while animating the position of the view.layer that you want to vibrate. Note that clipsToBounds for the view must be set to NO. Also be sure to include the QuartzCore framework in the project.

- (void)shakeView:(UIView *)view
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    animation.duration = 0.1;
    animation.byValue = @(20);
    animation.autoreverses = YES;
    animation.repeatCount = 10;
    [view.layer addAnimation:animation forKey:@"Shake"];
}
查看更多
登录 后发表回答