【问题标题】:How to make UIButton dragable but don't trigger click event如何使 UIButton 可拖动但不触发点击事件
【发布时间】:2013-04-20 08:58:03
【问题描述】:

我一般用这种方法来实现UIButton的可移动;

[button addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];

但它会同时触发touchUpInside事件,我需要touchUpInside事件来做一些其他的事情。

那么有人知道如何避免这种情况吗?

非常感谢;

使用下面的这些代码来解决它;

  [button addTarget:self action:@selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];

    [button addTarget:self action:@selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [button addTarget:self action:@selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];

    - (void) touchUpInside :(UIControl*)c withEvent:ev{
    NSLog(@"touch Up inside");
    if (count) {
        NSLog(@"here I can do something about clicking event");
    }
}

    - (void) touchDown:(UIControl*)c withEvent:ev{
    NSLog(@"touch Down");
    count = YES;
}

    -(void) touchDragInside:(UIControl*)c withEvent:ev{
    NSLog(@"touch drag inside");
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
    count = NO;
}

【问题讨论】:

  • 查看可能的duplicate for this它会帮助你。
  • 解决了它。我只是在 touchUpInside 事件中做一个标志。

标签: ios uibutton


【解决方案1】:

实际上实现这一点的最佳方法是使用 UIPanGestureRecognizer 并将其添加到按钮中。它可以很好地处理拖动,您也可以点击它而无需执行任何其他操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2013-03-10
    • 2014-02-20
    • 2015-04-23
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多