IOS app Icon Long Press the implementation of the Jitter effect (caanimation)

Source: Internet
Author: User

Effect: When long press the icon, the icon jitter, click outside the icon to stop the jitter

Project download: GitHub project Download

 @interface viewcontroller (){Uiimageview*imageview;}@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; ImageView = [[UiimageviewAlloc] Initwithframe:cgrectmake (0,0, -, -)]; ImageView. Center= Self. View. Center; ImageView. Image= [UIImageimagenamed:@"Icon1"]; ImageView. userinteractionenabled=YES; [ Self. ViewAddsubview:imageview];//Add long press gestureUilongpressgesturerecognizer *recognize = [[Uilongpressgesturerecognizer alloc] Initwithtarget: SelfAction@selector(longpress)];//Long Press response timeRecognize. Minimumpressduration=1; [ImageView addgesturerecognizer:recognize];additional setup after loading the view, typically from a nib.}

Gesture response

- (void)longPress {    CABasicAnimation *animation = (CABasicAnimation *)[imageView.layer animationForKey:@"rotation"];    ifnil) {        [self shakeImage];    }else {        [self resume];    }}
//If you click on the outside of the icon, stop dithering- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {Uitouch *touch = [touches anyobject]; Cgpoint point = [Touch LocationInView:self.view];//Convert the coordinate system to determine if the touch point is inside the ImageView, and in the words, still jitter, otherwise stop ditheringCgpoint p = [Self.view convertpoint:point Toview:imageview];if(! [ImageView pointinside:p withevent:event]) {NSLog (@ "xxxxxxx");    [Self pause]; }}//layer.speed/* the rate ofThe layer. Used toScale parent Time tolocal time, e.g. *ifRate is2, local time progresses twice asFast asParent time. * Defaults to 1.*/The understanding of this parameter is more complicated, my understanding is that the time of the layer and the time of the parent layer relative speed, the same speed as 1 o'clock, for 2 then the parent layer over a second, and the layer is over two seconds (two seconds animation), 0 is still. - (void) Pause {imageView.layer.speed =0.0;} - (void) Resume {imageView.layer.speed =1.0;}

Animation settings

- (void) Shakeimage {//Create animated object, rotate around Z axisCabasicanimation *animation = [Cabasicanimation animationwithkeypath:@"Transform.rotation.z"];//Set properties, period duration[Animation setduration:0.08];//Jitter angleAnimation. Fromvalue= @ (-m_1_pi/2); Animation. Tovalue= @ (m_1_pi/2);//Repeat number, infinite sizeAnimation. RepeatCount= Huge_val;//Return to the originalAnimation. Autoreverses=YES;//Anchor point set to Image Center, jitter around centerImageView. Layer. Anchorpoint= Cgpointmake (0.5,0.5); [ImageView. LayerAddanimation:animation forkey:@"Rotation"];}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS app Icon Long Press the implementation of the Jitter effect (caanimation)

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.