iOS 15th day (3, event handling, gesture handling)

Source: Internet
Author: User

7> gesture Recognition
Use Uiimageview reason: Before can see the picture, and can listen to click Only UIButton, learned the gesture, our uiimageview can also.
* TAP (Agent: The left can not point, to the right to point)
* Longpress (allowablemovement: Maximum moving range before triggering)
> Default call two times, start once, end once.
* Swipe: (a gesture can only recognize One direction)
Rotation
Based on the last rotation
* Reset: (gesture value is relative to the original position, we should be required relative to the last time, so each call, then reset, each time is zero start rotation angle)
Zoom: RESET
* How can I support both rotation and scaling? By default, multiple fingers are not supported,
Simultaneously: at the same time
The proxy's simultaneously method is invoked when a gesture is used, asking if multiple gestures are supported
* Pan
Get pan Position: Translationinview
Reset: Settranslation:inview: You need to pass a view, because the position of the point is related to the coordinate system, see if he is based on which coordinate system is emptied.

#import "HMViewController.h"@interfaceHmviewcontroller () <UIGestureRecognizerDelegate>@property (Weak, nonatomic) Iboutlet Uiimageview*Imagview;@end@implementationHmviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //PanUipangesturerecognizer *pan =[[Uipangesturerecognizer alloc] initwithtarget:self action: @selector (pan:)]; [_imagview Addgesturerecognizer:pan];}- (void) Pan: (Uipangesturerecognizer *)pan{//get the position of the finger movementCgpoint trans =[Pan Translationinview:_imagview]; _imagview.transform=cgaffinetransformtranslate (_imagview.transform, trans.x, TRANS.Y); //Reset[Pan Settranslation:cgpointzero Inview:_imagview]; NSLog (@"%@", Nsstringfromcgpoint (trans)); }#warningpinch-(void) addpinch{Uipinchgesturerecognizer*pinch =[[Uipinchgesturerecognizer alloc] initwithtarget:self action: @selector (pinch:)]; //reason for setting up proxy: Want to support multiple gestures simultaneouslyPinch.Delegate=Self ;    [_imagview Addgesturerecognizer:pinch]; }- (void) Pinch: (Uipinchgesturerecognizer *) pinch{_imagview.transform=Cgaffinetransformscale (_imagview.transform, Pinch.scale, Pinch.scale); //ResetPinch.scale =1;}//simultaneous: At the same time//by default, multiple gestures are not supported//when you use a gesture, you call-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) othergesturerecognizer{returnYES;}#warningrotation-(void) addrotation{//RotationUirotationgesturerecognizer *rotation =[[Uirotationgesturerecognizer alloc] initwithtarget:self action: @selector (rotation:)]; Rotation.Delegate=Self ; [_imagview addgesturerecognizer:rotation];}- (void) Rotation: (Uirotationgesturerecognizer *) rotation{NSLog (@"%f", rotation.rotation);//_imagview.transform = cgaffinetransformmakerotation (rotation.rotation);_imagview.transform =cgaffinetransformrotate (_imagview.transform, rotation.rotation); //ResetRotation.rotation =0;}#warningswipe-(void) addswipe{//Swipe//a gesture can only recognize One DirectionUiswipegesturerecognizer *swipe =[[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (swipe:)]; Swipe.direction=Uiswipegesturerecognizerdirectionright; [_imagview addgesturerecognizer:swipe];}- (void) Swipe: (Uiswipegesturerecognizer *) swipe{NSLog (@"Swipe");}#warninglongpress-(void) addlongpress{//longpressUilongpressgesturerecognizer *longpress =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (longpress:)]; [_imagview addgesturerecognizer:longpress];}- (void) Longpress: (Uilongpressgesturerecognizer *) longpress{//performing events based on status    if(Longpress.state = =Uigesturerecognizerstatebegan) {NSLog (@"longpress"); }}#warningtap-(void) addtap{//TapUITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tap:)]; //tap how many times to trigger gestures//tap.numberoftapsrequired = 2; //    //    //how many fingers must touch to trigger a gesture//tap.numberoftouchesrequired = 2;tap.Delegate=Self ; [_imagview Addgesturerecognizer:tap];}//can this touch point trigger gestures//-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) Touch//{//cgpoint currentpoint = [Touch Locationinview:_imagview];//    //if (Currentpoint.x < _imagview.bounds.size.width * 0.5) {//return NO;//}else{//        //return YES;//    }//}- (voidTap: (UITapGestureRecognizer *) tap{NSLog (@"Tap");}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

iOS 15th day (3, event handling, gesture handling)

Related Article

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.