IOS adds the jitter effect to the view and ios view adds the jitter

Source: Internet
Author: User

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 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.