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)