IOS adds the jitter effect to the view and ios view adds the jitter
The jitter effect is rarely used in development, but sometimes it has a good installation effect, such as some user error operations.
The effect is as follows, but the effect of gif is not really nice.
Code on
1-(void) shakeAnimationForView :( UIView *) view 2 3 {4 // obtain the current View 5 6 CALayer * viewLayer = view. layer; 7 8 // obtain the current View position 9 10 CGPoint position = viewLayer. position; 11 12 // move two endpoints 13 14 CGPoint x = CGPointMake (position. x + 10, position. y); 15 16 CGPoint y = CGPointMake (position. x-10, position. y); 17 18 // set animation 19 20 CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "position"]; 21 22 // set the Motion Form 23 24 [animation setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionDefault]; 25 26 // set the start position 27 28 [animation setFromValue: [NSValue valueWithCGPoint: x]; 29 30 // set end position 31 32 [animation setToValue: [NSValue valueWithCGPoint: y]; 33 34 // set automatic reversal 35 36 [animation setAutoreverses: YES]; 37 38 // set the time to 39 40 [animation setDuration :. 06]; 41 42 // set the number of times 43 44 [animation setRepeatCount: 3]; 45 46 // Add the animation 47 48 [viewLayer addAnimation: animation forKey: nil]; 49 50 51 52}
You only need to upload the view to the desired location.
For example:
1 view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 50, 50)]; 2 view1.backgroundColor = [UIColor blueColor]; 3 view1.layer.cornerRadius = 25; 4 [self.view addSubview:view1]; 5 6 } 7 8 - (IBAction)beginView:(id)sender { 9 [self shakeAnimationForView:view1];10 }