iOS Development small feature eight: simple use of gestures (6 types) and Proxy methods

Source: Internet
Author: User

Code:

1 #import "ViewController.h"2 @interfaceViewcontroller () <UIGestureRecognizerDelegate>4@property (Weak, nonatomic) Iboutlet Uiimageview *ImageView;5 @end7 @implementationViewcontroller8- (void) Viewdidload {9 [Super Viewdidload];Ten     //as long as the gesture, the default is relative to the original location, and the default control only support a gesture!! If you want to support multiple gestures, see the proxy method below One //[self setuprotation]; A //[self setuppinch]; - [self setuppan]; - } the  - #pragmaMark-Tap gestures -- (void) Setuptap - { +     //Create a tap gesture -UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)]; +     //requires 3 consecutive points to trigger A     //tap.numberoftapsrequired = 3; at     //add a tap gesture to the ImageView - [Self.imageview Addgesturerecognizer:tap]; -     //setting up a point-by-gesture proxy -Tap.Delegate=Self ; - } -  in- (voidTap: (UITapGestureRecognizer *) Tap - { toNSLog (@"%s", __func__); + } -  the #pragmaMark-long-Press gestures * //default trigger two times (start touch half a second, end touch once) $- (void) setuplongpressPanax Notoginseng { -Uilongpressgesturerecognizer *longpress =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (longpress:)]; the [Self.imageview addgesturerecognizer:longpress]; +Longpress.Delegate=Self ; A } the  +- (void) Longpress: (Uilongpressgesturerecognizer *) longpress - { $ at the beginning Touch $     if(Longpress.state = =Uigesturerecognizerstatebegan) { -NSLog (@"%s", __func__); -     } the at the end of the touch -     if(Longpress.state = =uigesturerecognizerstateended) {WuyiNSLog (@"%s", __func__); the     } - } Wu  - #pragmaMark-Swipe gestures About- (void) Setupswipe $ { -     //default swipe gesture to right-stroke screen -Uiswipegesturerecognizer *swipeup =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe:)]; -     //Stroke up ASwipeup.direction =Uiswipegesturerecognizerdirectionup; + [_imageview addgesturerecognizer:swipeup]; the     //a sweep gesture supports only one direction, and if you want to support multiple orientations, you must create multiple -Uiswipegesturerecognizer *swipedown =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe:)]; $     //Stroke down theSwipedown.direction =Uiswipegesturerecognizerdirectiondown; the [_imageview Addgesturerecognizer:swipedown]; the } the- (void) Swipe: (Uiswipegesturerecognizer *) Swipe - { inNSLog (@"%s", __func__); the } the  About #pragmaMark-Rotate gestures the //The default delivery angle is relative to the beginning of the position the- (void) Setuprotation the { +Uirotationgesturerecognizer *rotation =[[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector (rotation:)]; -Rotation.Delegate=Self ; the [Self.imageview addgesturerecognizer:rotation];Bayi } the- (void) Rotation: (Uirotationgesturerecognizer *) Rotation the { -     //let the picture rotate with the gesture rotation, the angle of rotation is equal to the value of the initial position, with rotation.rotation = 0; - //self.imageView.transform = cgaffinetransformmakerotation (rotation.rotation); the     //gesture rotation can make the picture rotate the n circle at high speed, and it is not controlled, the direction is not easy to determine. Mating rotation.rotation = 0; The rear angle is always 0 theSelf.imageView.transform =cgaffinetransformrotate (Self.imageView.transform, rotation.rotation);  the     //Reset theRotation.rotation =0; -     //get the angle of the gesture rotation theNSLog (@"%f", rotation.rotation); the } the 94 #pragmaMark-pinch gesture the //two finger zoom to zoom out picture the- (void) Setuppinch the {98Uipinchgesturerecognizer *pinch =[[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (pinch:)]; AboutPinch.Delegate=Self ; - [Self.imageview addgesturerecognizer:pinch];101 }102- (void) Pinch: (Uipinchgesturerecognizer *) Pinch103 {104     //The magnification ratio is relative to the original point, note: This cannot be used with Pinch.scale = 1, otherwise it cannot be enlarged the //self.imageView.transform = Cgaffinetransformmakescale (Pinch.scale, pinch.scale);106     //as the distance between the two fingers increases, the picture will quickly zoom in or out irregularly (there is no way to zoom in or out here), note: with Pinch.scale = 1, normal magnification and reduction107Self.imageView.transform =Cgaffinetransformscale (Self.imageView.transform, Pinch.scale, Pinch.scale);108     //Reset109Pinch.scale =1; theNSLog (@"%f", Pinch.scale);111 } the 113 #pragmaMark-dragging the //Mobile View the- (void) Setuppan the {117Uipangesturerecognizer *pan =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)];118 [Self.imageview Addgesturerecognizer:pan];119 } -- (void) Pan: (Uipangesturerecognizer *) Pan121 {122     //gets the movement of the gesture, relative to the original point123Cgpoint TRANSP =[Pan TranslationInView:self.imageView];124Self.imageView.transform =cgaffinetransformtranslate (Self.imageView.transform, transp.x, transp.y); the     //Reset126 [Pan Settranslation:cgpointzero InView:self.imageView];127NSLog (@"%@", Nsstringfromcgpoint (TRANSP)); - }129 @end

Proxy method:

1 allows multiple gestures to be supported at the same time, only one gesture is supported by default, to call this method note setting the proxy2-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer3 {4     returnYES;5 }6 7 whether to allow triggering gestures to start8-(BOOL) Gesturerecognizershouldbegin: (Uigesturerecognizer *) Gesturerecognizer9 {Ten     returnNO; One } A  - whether to allow the touch of the phone to be received (can control the range of touch) --(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch the { -     //gets the current touch point -Cgpoint CURRENTP =[Touch LocationInView:self.imageView]; - can accept touch in the left half of the picture +     if(Currentp.x < Self.imageView.bounds.size.width *0.5) { -         returnYES; +}Else { A         returnNO; at     } -}

iOS Development small feature eight: simple use of gestures (6 types) and Proxy methods

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.