Limit pan gestures to move view only within a circle
Effect:
Although it looks simple, the principle of implementation is a little bit more complicated-_-!!
The core place, is the need to calculate the pan gesture point and the distance of the specified point, can not exceed this distance, more than let the animation restore, it is easy to understand:)
////ROOTVIEWCONTROLLER.M//Circle////Copyright (c) 2014 y.x. All rights reserved.//#import "RootViewController.h"@interfaceRootviewcontroller ()@end@implementationRootviewcontroller- (void) viewdidload{[Super Viewdidload]; //a layer with a limited rangeCalayer *circlelayer =[Calayer layer]; Circlelayer.frame= (CGRect) {Cgpointzero, Cgsizemake ( -, -)}; Circlelayer.position=Self.view.center; Circlelayer.cornerradius= -/2. F; Circlelayer.opacity=0.5f; Circlelayer.backgroundcolor=[Uicolor Orangecolor]. Cgcolor; [Self.view.layer Addsublayer:circlelayer]; //Move gesturesUipangesturerecognizer *pan =[[Uipangesturerecognizer alloc] initwithtarget:self action: @se Lector (gestureevent:)]; //View for MobileUIView *move = [[UIView alloc] initWithFrame: (cgrect) {Cgpointzero, Cgsizemake ( -, -)}]; Move.backgroundcolor=[Uicolor Cyancolor]; Move.center=Self.view.center; Move.layer.cornerRadius= -/2. F; [Move Addgesturerecognizer:pan]; [Self.view addsubview:move];}- (void) Gestureevent: (Uipangesturerecognizer *) gesture{//get gesture coordinate pointsCgpoint translation =[gesture TranslationInView:gesture.view]; //Start if(Gesture.state = =Uigesturerecognizerstatebegan) {[UIView animatewithduration:0.2animations:^{Gesture.view.backgroundColor=[Uicolor Redcolor]; }]; } //State Change if(Gesture.state = =uigesturerecognizerstatechanged) {Gesture.view.center= Cgpointmake (gesture.view.center.x +translation.x, Gesture.view.center.y+TRANSLATION.Y); //calculates the distance between the point of the gesture and the specified coordinatesCgpoint Pointa =Gesture.view.center; Cgpoint POINTB=Self.view.center; CGFloat Distancex= pointa.x-pointb.x; CGFloat Distancey= Pointa.y-pointb.y; CGFloat distance= sqrt (Distancex*distancex + distancey*Distancey); //some actions when the distance is within 125.F if(Distance <= the. f) {[gesture Settranslation:cgpointzero InView:gesture.view]; } Else { //Turn off gestures first (do not allow users to continue interacting with gestures)gesture.enabled =NO; [UIView animatewithduration:0.2fanimations:^{Gesture.view.center=Self.view.center; Gesture.view.backgroundColor=[Uicolor Cyancolor]; } Completion:^(BOOL finished) {//turn the gesture again after the animation is finishedgesture.enabled =YES; }]; } } //End if(Gesture.state = =uigesturerecognizerstateended) {[UIView animatewithduration:0.2fanimations:^{Gesture.view.center=Self.view.center; Gesture.view.backgroundColor=[Uicolor Cyancolor]; }]; }}@end
At the core code:
1. Calculate Coordinate values
2. It is important to close the pan gesture and perform the animation when the distance exceeds the specified range, and then turn on the pan gesture after the animation is over.