iOS开发中的几个手势操作实例分享

2020-01-14 15:51:57于丽
易采站长站为您分析iOS开发中的几个手势操作实例分享,编写代码为传统的Objective-C,需要的朋友可以参考下  

手势操作---识别单击还是双击
在视图上同时识别单击手势和双击手势的问题在于,当检测到一个单击操作时,无法确定是确实是一个单击操作或者只是双击操作中的第一次点击。解决这个问题的方法就是:在检测到单击时,需要等一段时间等待第二次点击,如果没有第二次点击,则为单击操作;如果有第二次点击,则为双击操作。
检测手势有两种方法,一种是定制子视图,重写视图从UIResponder类中继承来的事件处理方法,即touchesBegan:withEvent:等一系列方法来检测手势;另一个方法是使用手势识别器,即UIGestureRecognizer的各种具体子类。
一.重写事件处理方法

 

复制代码

 

- (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];  
    //...