Gesture Operation---Recognition click or double-click
The problem with recognizing both the click Gesture and the double-click gesture on the view is that when a click is detected, it is not certain that it is indeed a click or just the first click in the double-click operation. The way to solve this problem is to wait for a second click when a click is detected, or click if there is no second click, or double-click if there is a second click.
There are two ways to detect gestures, one is customizing a child view, overriding the event-handling method inherited by the view from the Uiresponder class, that is, Touchesbegan:withevent: A series of methods to detect gestures; Another method is to use gesture recognizers, That is, the various specific subclasses of Uigesturerecognizer.
I. Overriding the event-handling method
Copy Code code as follows:
-(ID) init {
if (self = [super init]) {
self.userinteractionenabled = YES;
self.multipletouchenabled = YES;
// ...
}
return self;
}
-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
[NSObject cancelpreviousperformrequestswithtarget:self];
Uitouch *touch = [touches anyobject];
Cgpoint touchpoint = [Touch locationinview:self];
if (Touch.tapcount = = 1) {
[Self performselector: @selector (handlesingletap:) withobject:[nsvalue Valuewithcgpoint:touchpoint] afterDelay:0.3] ;
}else if (Touch.tapcount = 2)
{
[Self handledoubletap:[nsvalue valuewithcgpoint:touchpoint]];
}
}
-(void) Handlesingletap: (nsvalue*) Pointvalue
{
Cgpoint touchpoint = [Pointvalue cgpointvalue];
//...
}
-(void) Handledoubletap: (nsvalue*) Pointvalue
{
Cgpoint touchpoint = [Pointvalue cgpointvalue];
//...
}
First confirm that the userinteractionenabled and Multipletouchenabled properties for the custom view are yes.
In the Touchesended:withevent: method, if the first touch ends, the Cancelpreviousperformrequestswithtarget: The method does not work because self does not schedule any methods. At this point the Tapcount is 1, using PerformSelector:withObject:afterDelay: Invoke the Click Event-handling method to execute after 0.3s clock.
Copy Code code as follows:
[Self performselector: @selector (handlesingletap:) withobject:[nsvalue Valuewithcgpoint:touchpoint] afterDelay:0.3] ;
If this is a click, there will be no more touch events in the next 0.3 minutes, then Handlesingletap: The method executes so that the click action is recognized.
If this is a double-click, the second click is triggered within 0.3s, in the second touch operation of the Touchesended:withevent: method, Cancelpreviousperformrequestswithtarget: First, the Handlesingletap: Method's schedule is canceled before it is executed, and then the Handledoubletap: method handles the double-click operation.
two. Use of gesture recognizer
Using gesture recognizer recognition can be a lot simpler, just add two gesture recognizers, detect the click and double-click Events separately, and set the necessary properties.
Copy Code code as follows:
-(ID) init {
if (self = [super init]) {
self.userinteractionenabled = YES;
UITapGestureRecognizer *singletapgesture = [UITapGestureRecognizer alloc]initwithtarget:self action: @selector ( Handlesingletap:)];
singletapgesture.numberoftapsrequired = 1;
singletapgesture.numberoftouchesrequired = 1;
[Self addgesturerecognizer:singletapgesture];
UITapGestureRecognizer *doubletapgesture = [UITapGestureRecognizer alloc]initwithtarget:self action: @selector ( Handledoubletap:)];
doubletapgesture.numberoftapsrequired = 2;
doubletapgesture.numberoftouchesrequired = 1;
[Self addgesturerecognizer:doubletapgesture];
[Singletapgesture Requiregesturerecognizertofail:doubletapgesture];
}
return self;
}
-(void) Handlesingletap: (Uigesturerecognizer *) sender{
Cgpoint touchpoint = [Sender locationinview:self];
//...
}
-(void) Handledoubletap: (Uigesturerecognizer *) sender{
Cgpoint touchpoint = [Sender locationinview:self];
//...
}
The only thing to be aware of
Copy Code code as follows:
[Singletapgesture Requiregesturerecognizertofail:doubletapgesture];
The meaning of this sentence, only when the doubletapgesture recognition failed (that is, recognize that this is not a double-click operation), singletapgesture can begin to identify, and we started with the same problem.
Uigesturerecognizer Small Application
1. Pat gesture: Double finger, click, modify ImageView frame (0,0,320,200)
2, long by finger: single finger, modify the ImageView alpha=0.5
3, to achieve translation, rotation, kneading
4, light sweep: Vertical light sweep implementation diagram: like random switch display; horizontal light sweep implementation: Image disappears, randomly modifies imageview background color
5, ImageView can only add a gesture recognizer at a time.
Copy Code code as follows:
#define _originalrect CGRectMake (10, 50, 300, 450)
#define _ORIGINALIMAGENAME @ "H4.jpeg"
#import "HMTRootViewController.h"
@interface Hmtrootviewcontroller () {
UITapGestureRecognizer * _tapgesture;
Uilongpressgesturerecognizer * _longgesture;
Uipangesturerecognizer * _pangesture;
Uirotationgesturerecognizer * _rotategesture;
Uipinchgesturerecognizer * _pinchgesture;
Uiswipegesturerecognizer * _verticalswipegesture;
Uiswipegesturerecognizer * _horizontanlswipegesture;
BOOL Istopdownofrightleft; Vertical sliding is yes, horizontal sliding is no
}
@property (nonatomic,retain) UIButton * button;
@property (nonatomic,retain) Uiimageview * ImageView;
@end
@implementation Hmtrootviewcontroller
-(void) dealloc{
Release_safely (_imageview);
Release_safely (_button);
[Super Dealloc];
}
-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil
{
self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];
if (self) {
Custom initialization
Istopdownofrightleft = YES;
}
return self;
}
-(void) viewdidload
{
[Super Viewdidload];
Do no additional setup after loading the view.
[Self createbuttonview];
[Self createimageview];
}
#pragma mark-Set image
-(void) createimageview{
Self.imageview = [[Uiimageview alloc]initwithimage:[uiimage Imagenamed:_originalimagename]];
_imageview.frame = CGRectMake (10, 50, 300, 450);
_imageview.userinteractionenabled = YES;
[Self.view Addsubview:_imageview];
[_imageview release];
}
#pragma mark-Set gestures
#pragma mark, click the gesture.
-(void) createtapgesturerecognizer{
_tapgesture = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (Tapgesturerecognizer:)];
_tapgesture.numberoftapsrequired = 1;
_tapgesture.numberoftouchesrequired = 2;
[Self.imageview Addgesturerecognizer:_tapgesture];
[_tapgesture release];
}
-(void) Tapgesturerecognizer: (UITapGestureRecognizer *) tapgesture{
Self.imageView.frame = CGRectMake (0, 0, 320, 200);
NSLog (@ "%@", Nsstringfromcgrect (Self.imageView.frame));
}
#pragma mark, press the gesture.
-(void) createlonggesturerecognizer{
_longgesture = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (Longgesturerecognizer:)];
_longgesture.numberoftouchesrequired = 1;
_longgesture.minimumpressduration = 1.0;
[Self.imageview Addgesturerecognizer:_longgesture];
[_longgesture release];
}
-(void) Longgesturerecognizer: (Uilongpressgesturerecognizer *) longgesture{
Self.imageView.alpha = 0.5;
NSLog (@ "%s", __function__);
}
#pragma mark Pan drag gesture
-(void) createpangesturerecognizer{
_pangesture = [[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector (Pangesturerecognizer:)];
[Self.imageview Addgesturerecognizer:_pangesture];
[_pangesture release];
}
-(void) Pangesturerecognizer: (Uipangesturerecognizer *) pangesture{
NSLog (@ "%s", __function__);
Cgpoint txty = [Pangesture TranslationInView:self.view];
Self.imageView.transform = Cgaffinetransformtranslate (Self.imageView.transform, txty.x, TXTY.Y);
[Pangesture settranslation:cgpointmake (0, 0) InView:self.view];
}
#pragma mark, rotate the gesture.
-(void) createrotationgesturerecognizer{
_rotategesture = [[Uirotationgesturerecognizer alloc]initwithtarget:self Action: @selector ( Rotationgesturerecognizer:)];
[Self.imageview Addgesturerecognizer:_rotategesture];
[_rotategesture release];
}
-(void) Rotationgesturerecognizer: (Uirotationgesturerecognizer *) rotategesture{
NSLog (@ "%s", __function__);
Self.imageView.transform = Cgaffinetransformrotate (Self.imageView.transform, rotategesture.rotation);
rotategesture.rotation = 0;
}
#pragma mark kneading zoom gesture
-(void) createpinchgesturerecognizer{
_pinchgesture = [[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector (Pinchgesturerecognizer:)];
[Self.imageview Addgesturerecognizer:_pinchgesture];
[_pinchgesture release];
}
-(void) Pinchgesturerecognizer: (Uipinchgesturerecognizer *) pinchgesture{
NSLog (@ "%s", __function__);
Self.imageView.transform = Cgaffinetransformscale (Self.imageView.transform, Pinchgesture.scale, Pinchgesture.scale );
Pinchgesture.scale = 1;
}
#pragma mark--a light sweep gesture.
#pragma mark Vertical and horizontal sweep
-(void) createverticalswipegesturerecognizer{
_verticalswipegesture = [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector ( Swipegesturerecognizer:)];
_verticalswipegesture.direction = Uiswipegesturerecognizerdirectionup | Uiswipegesturerecognizerdirectiondown;
[Self.imageview Addgesturerecognizer:_verticalswipegesture];
[_verticalswipegesture release];
}
#pragma mark Horizontal Sweep
-(void) createhorizontanlswipegesture{
_horizontanlswipegesture = [[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector ( Swipegesturerecognizer:)];
_horizontanlswipegesture.direction = Uiswipegesturerecognizerdirectionleft | Uiswipegesturerecognizerdirectionright;
[Self.imageview Addgesturerecognizer:_horizontanlswipegesture];
}
-(void) Swipegesturerecognizer: (Uiswipegesturerecognizer *) swipegesture{
NSLog (@ "%s", __function__);
if (swipegesture.direction = = (uiswipegesturerecognizerdirectionup| Uiswipegesturerecognizerdirectiondown)) {
Self.imageView.image = [UIImage imagenamed:[nsstring stringwithformat:@ "H%i.jpeg", Arc4random ()%7+1]];
;
}else if (swipegesture.direction = = (uiswipegesturerecognizerdirectionleft| Uiswipegesturerecognizerdirectionright)) {
Self.imageView.image = nil;
Self.imageView.backgroundColor = [Uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ()%256/255.0 Blue: Arc4random ()%256/255.0 alpha:1.0];
}
}
#pragma mark-Set button
-(void) createbuttonview{
Nsarray * Buttonarray = @[@ "Light point", @ "long press", @ "Pan", @ "rotate", @ "kneading", @ "Light sweep"];
for (int i = 0; i < [Buttonarray count]; i++) {
Self.button = [UIButton Buttonwithtype:uibuttontypesystem];
_button.frame = CGRectMake (10+50*i, 500, 50, 48);
[_button Settitle:[buttonarray objectatindex:i] forstate:uicontrolstatenormal];
[_button addtarget:self Action: @selector (Onclikbutton:) forcontrolevents:uicontroleventtouchupinside];
_button.tag = i;
[Self.view Addsubview:_button];
}
}
-(void) Onclikbutton: (UIButton *) button{
[Self resetimageview];
Switch (Button.tag) {
Case 0:
[Self createtapgesturerecognizer];
Break
Case 1:
[Self createlonggesturerecognizer];
Break
Case 2:
[Self createpangesturerecognizer];
Break
Case 3:
[Self createrotationgesturerecognizer];
Break
Case 4:
[Self createpinchgesturerecognizer];
Break
Case 5:
if (Istopdownofrightleft = YES) {
[Self createverticalswipegesturerecognizer];
Istopdownofrightleft = NO;
} else {
[Self createhorizontanlswipegesture];
Istopdownofrightleft = YES;
}
Break
Default
Break
}
}
#pragma mark-Reset ImageView
-(void) Resetimageview
{
for (int i = 0; i < [self.imageView.gestureRecognizers count]; i++) {
[Self.imageview removegesturerecognizer:[self.imageview.gesturerecognizers objectatindex:i]];
}
Self.imageView.alpha = 1.0;
Self.imageView.transform = cgaffinetransformidentity;
Self.imageView.frame = _originalrect;
Self.imageView.image = [UIImage imagenamed:_originalimagename];
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
@end