简体   繁体   English

在子视图控制器上禁用Tap手势?如何确定它是PanGesture还是TapGesture?

[英]Disable Tap Gesture on Child View Controller? How to determine Is it a PanGesture or TapGesture?

All. 所有。

In my iOS 8.0 app. 在我的iOS 8.0应用程序中。

In a Parent Child View architecture. 在父子视图架构中。 By this code... 通过这段代码......

[self addChildViewController:slideTableViewController];
[self.view addSubview:slideTableViewController.view];
[slideTableViewController didMoveToParentViewController:self];

I have implemented TapGesturerecognizer & PanGesturerecognizer on Base View Controller. 我在Base View Controller上实现了TapGesturerecognizer和PanGesturerecognizer。

So, that it can recognise Pan(Dragging) and Tap. 因此,它可以识别Pan(拖动)和Tap。 I need both gestures on my BaseView. 我在BaseView上需要两个手势。

Just do not want Tap Gesture on SlideView . 只是不想在 SlideView SlideView Tap Gesture As I want to execute didSelectRowAtIndexpath method on child view, 因为我想在子视图上执行didSelectRowAtIndexpath方法,

Solution: 解:

Answer for Question 1: Many StackOverflow answers have the same funda.. Disable Tap gesture when your touch encounters child view. 对问题1的回答:许多StackOverflow答案具有​​相同的基础。当您的触摸遇到子视图时,禁用点击手势。

if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
                if(CGRectContainsPoint(slideTableViewController.view.frame,[touch locationInView:self.view])){
                    return NO;
     }
}

Answer for Question 2: 问题2的答案:

How to determine Is it a PanGesture or TapGesture ? 如何确定它是PanGesture还是TapGesture?

For each gesture type the delegate method will call 对于每个手势类型,委托方法将调用

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

If you have implemented two gestures in your view, pan gesture and touch gesture, 如果您在视图中实现了两个手势,则平移手势和触摸手势,

This method will call 2 times, 1 time for pan gesture and 1 time for tap gesture. 该方法将调用2次,平移手势1次,轻击手势1次。

So, in this method you can check like isKindOfClass method. 因此,在此方法中,您可以检查isKindOfClass方法。

Thank you very much for helping...... 非常感谢您的帮助......

在此输入图像描述

只需设置tapGesture.cancelsTouchesInView = YES;

You can implement the gesture's delegate method in your baseViewController : 您可以在baseViewController实现手势的委托方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldReceiveTouch:(UITouch *)touch {
    return touch.view == self.view;
}

OR //If it is a Tap Gesture and Your touch intersects with a perticular View, For that we have this method. 或者//如果它是一个Tap Gesture,你的触摸与一个特定的视图相交,为此我们有这个方法。

if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
                if(CGRectContainsPoint(slideTableViewController.view.frame,[touch locationInView:self.view])){
                    return NO;
     }
}

I have done this in UIViews I hope this could help you: 我在UIViews中这样做了我希望这可以帮助你:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [touches anyObject];
[touch locationInView:self.view];
if(touch.view == self.topView )
{
     //just return
}
if(touch.view == SOSTopView )
{
   //just return
}
}

Looks like you should implement this method of UITableViewDelegate : 看起来你应该实现UITableViewDelegate这个方法:

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // you can add some logic here
    return nil;
}

This will prevent from selecting any rows. 这将阻止选择任何行。

Isn't there a userInteractionEnabled property that you can make use of? 是否有可以使用的userInteractionEnabled属性? So you could do the following: 所以你可以做到以下几点:

slideTableViewController.userInterationEnabled = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM