移除 UIButton 的手势

Posted

技术标签:

【中文标题】移除 UIButton 的手势【英文标题】:remove gesture for UIButton 【发布时间】:2014-04-10 07:03:08 【问题描述】:

我正在使用UIGestureRecognizer 进行平移、旋转、捏合。但我正在申请整个视图。我需要删除子视图以外的按钮的手势。但是当我使用平移按钮也会影响。如何限制按钮从 self.view 移动。我在UIPanGestureRecognizer 中使用了下面的代码。

UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                        action:@selector(ondbPan:)];

[self.view addGestureRecognizer:dbpan];

[closeButton removeGestureRecognizer:dbpan];

潘:

- (void)ondbPan:(UIPanGestureRecognizer *)gesture

    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) 


        CGPoint offset = [gesture translationInView:self.view];

        CGPoint center = gesture.view.center;
        center.x += offset.x;
        center.y += offset.y;
        gesture.view.center = center;

        [gesture setTranslation:CGPointZero inView:self.view];

    

【问题讨论】:

【参考方案1】:

如果该子视图属于 UIButton 类,请尝试使用下面的代码,UIGestureRecognizer 的委托返回 FALSE。在您设置和添加UIPanGestureRecognizer 时还设置委托dbpan.delegate = self;

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

      if ([touch.view isKindOfClass:[UIButton class]])
      
           return FALSE;
      
      else
      

         return TRUE;
      

更新:-

我不知道为什么这对你不起作用我也使用以下代码测试了这个创建一个演示:-

.h 文件

@interface myviewcontroller : UIViewController<UIGestureRecognizerDelegate>

和 .m 类

- (void)pan:(UIPanGestureRecognizer *)gesture

    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) 

        CGPoint location = [gesture locationInView:self.view];

        [demoView setCenter:location];
    


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

    if ([touch.view isKindOfClass:[UIButton class]])
    
        return FALSE;
    
    else
    

        return TRUE;
    


- (void)viewDidLoad



    UIPanGestureRecognizer *dbpan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(pan:)];
    dbpan.delegate=self;
    [self.view addGestureRecognizer:dbpan];
    [super viewDidLoad];


-(IBAction)B1called


    NSLog(@"This is called button 1");


-(IBAction)B2called

     NSLog(@"This is called button 2");


它的输出是

【讨论】:

这对我不起作用。你能检查一下我更新的 Pan 代码吗?【参考方案2】:

在 Swift 上:

funcgestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool

if (touch.view!.isKindOfClass(UIButton)) 
    return false

return true

注意:确保添加 UIGestureRecognizerDelegate 并在 viewDidLoad() 上点击Gesture.delegate = self。

【讨论】:

【参考方案3】:

试试这个...

while (closeButton.gestureRecognizers.count) 
    [closeButton removeGestureRecognizer:[closeButton.gestureRecognizers objectAtIndex:0]];

【讨论】:

以上是关于移除 UIButton 的手势的主要内容,如果未能解决你的问题,请参考以下文章

使用滑动手势交换两个 UIButton

从文本字段中移除焦点

UIView 处理手势,内部 UIButton 应该捕捉触摸

UIButton检测按住,以及“手势识别器”?

如何从 UIScrollView 内的 UIButton 传递延迟滚动手势?

识别禁用的 UIButton 上的单击或手势事件