簡體   English   中英

點擊手勢+長按手勢無法同時使用

[英]Tap Gesture + Long Press Gesture both not working Together

我想在視圖中一起使用輕擊手勢和長按手勢。 但我的問題是,我無法在點擊時運行輕擊手勢動作。 但是長按手勢效果很好。

這是代碼片段。

            UILongPressGestureRecognizer *longPressGesture=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(ontappLongPressGesture:)];
            longPressGesture.minimumPressDuration=0.6;

            longPressGesture.delegate=self;
            [cell.view addGestureRecognizer:longPressGesture];

            UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellSelected:)];
            //[gesture requireGestureRecognizerToFail:longPressGesture]; //I have tried with this line also but not working
            gesture.delegate=self;
            [cell.view addGestureRecognizer:gesture];

我也設置了委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}

長按會調用此方法

- (void)ontappLongPressGesture:(id)sender{

    //Long press code here

}

但是這種方法不會在點擊時被調用

-(void)cellSelected:(id)sender {

     //Single tap code here

}

您還沒有指定將這些gestureRecognizer放在哪種類型的視圖上,但是因為您將其稱為“單元格”,我假設它在UITableView上?

如果需要,您需要確保設置cancelsTouchesInView標志:

gesture.cancelsTouchesInView=NO;

您需要使用以下兩種方法之一。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // test if our control subview is on-screen
    if (cell.view.superview != nil) {
        if ([touch.view isDescendantOfView:cell.view]) {
            // we touched our control surface
            return YES; // handle the touch
        }
    }
    return NO; // ignore the touch
}

在這里,您需要指定想要的姿態手勢視圖。

或者您也可以使用這些代碼行

gesture.cancelsTouchesInView = NO; 
longPressGesture.cancelsTouchesInView = NO;

希望它會對你有所幫助。

據我了解,您對UITableViewCell使用了這些手勢。 你為什么不使用tableView:didSelectRowAtIndexPath: ?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM