Limit pan gestures to move view only within a circle

Source: Internet
Author: User

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.

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.