How to make “shaking” animation

后端 未结 4 1080
栀梦
栀梦 2021-02-01 07:41

In my xcode project I have an UIImage. I want to make it shaking (animation) from left to right a little bit. I wrote this code, however, its not working.

-(void         


        
4条回答
  •  萌比男神i
    2021-02-01 08:06

    Here's the code I use for that effect.

     -(void)shakeView {
    
            CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
            [shake setDuration:0.1];
            [shake setRepeatCount:2];
            [shake setAutoreverses:YES];
            [shake setFromValue:[NSValue valueWithCGPoint:
                                     CGPointMake(lockImage.center.x - 5,lockImage.center.y)]];
            [shake setToValue:[NSValue valueWithCGPoint:
                                   CGPointMake(lockImage.center.x + 5, lockImage.center.y)]];
            [lockImage.layer addAnimation:shake forKey:@"position"];
        }
    

    As Jere mentioned. Make sure to include the import:

    #import 
    

    You can also add it as a category on UIView.

提交回复
热议问题