美文网首页
iOS 手势识别 - gestureRecognizer

iOS 手势识别 - gestureRecognizer

作者: gezhenrong | 来源:发表于2017-04-28 11:23 被阅读20次

    iOS设备的手势操作,在app中是个必须且实用的功能,为app加分不少。

    向左向右轻划

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self  action:@selector(handleSwipeLeft)];  
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;  
    [self.view addGestureRecognizer:swipeLeft];  
    [swipeLeft release];  
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]   
                                             initWithTarget:self   
                                                     action:@selector(handleSwipeRight)];  
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;  
    [self.view addGestureRecognizer:swipeRight];  
    [swipeRight release];  
    

    单指单击

    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self   
                      action:@selector(handleSingleFingerEvent:)];  
    singleFingerOne.numberOfTouchesRequired = 1; //手指数  
    singleFingerOne.numberOfTapsRequired = 1; //tap次数  
    singleFingerOne.delegate = self;  
    [self.view addGestureRecognizer:singleFingerOne];  
    

    单指双击

    UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self   action:@selector(handleSingleFingerEvent:)];  
    singleFingerTwo.numberOfTouchesRequired = 1;  
    singleFingerTwo.numberOfTapsRequired = 2;  
    singleFingerTwo.delegate = self;  
    [self.view addGestureRecognizer:singleFingerTwo];  
    

    双指单击和双击

     UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self   
                                action:@selector(handleDoubleFingerEvent:)];  
     doubleFingerOne.numberOfTouchesRequired = 2;  
    doubleFingerOne.numberOfTapsRequired = 1;  
    doubleFingerOne.delegate = self;  
    UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc]  initWithTarget:self  action:@selector(handleDoubleFingerEvent:)];  
    doubleFingerTwo.numberOfTouchesRequired = 2;  
    doubleFingerTwo.numberOfTapsRequired = 2;  
    doubleFingerTwo.delegate = self;  
    [self.view addGestureRecognizer:doubleFingerOne];  
    [self.view addGestureRecognizer:doubleFingerTwo];  
    
    //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理  
    [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];  
    //同理双指亦是如此  
    [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];  
    

    事件处理方法:

     - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender  
     {  
        if (sender.numberOfTapsRequired == 1) {  
            //单指单击  
            NSLog(@"单指单击");  
        }else if(sender.numberOfTapsRequired == 2){  
            //单指双击  
            NSLog(@"单指双击");  
         }  
     }  
     //处理双指事件  
     - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender  
     {  
        if (sender.numberOfTapsRequired == 1) {  
             //双指单击  
             NSLog(@"双指单击");  
         }else if(sender.numberOfTapsRequired == 2){  
             //双指双击  
             NSLog(@"双指双击");  
         }  
    } 
    

    相关文章

      网友评论

          本文标题: iOS 手势识别 - gestureRecognizer

          本文链接:https://www.haomeiwen.com/subject/cpmozttx.html