【问题标题】:How to get a UIButton's UIControlTouchDragInside and UIControlTouchDragOutside threshold to be the frame?如何让 UIButton 的 UIControlTouchDragInside 和 UIControlTouchDragOutside 阈值成为框架?
【发布时间】:2026-01-08 08:30:02
【问题描述】:

UIButton 的UIControlEventTouchDragInsideUIControlEventTouchDragOutside 阈值似乎在按钮框架之外一些未知量。这个距离似乎大约是手指宽度的大小。有没有办法让这个阈值成为没有这个手指宽度缓冲区的实际帧/边界?

这在 iPad 上不是问题,但在 iPhone 和较小的空间中,它会导致我的自定义控件偶尔工作。

谢谢

【问题讨论】:

  • 好吧,CGSize treshold = CGSizeMake(widthOfYourFingerInPixels, widthOfYourFingerInPixels);
  • DragInsideEvent 一直触发到按钮框架之外一段距离。我希望 DragInsideEvent 会触发,直到它到达按钮框架的边缘。然后 DragOutsideEvent 将触发。这不会发生。

标签: iphone ios uibutton uicontrol


【解决方案1】:

作为一种解决方法,我正在执行以下操作。

我已经更改了我的 UIControlEventTouchUpInsideUIControlEventTouchUpOutside 的处理程序。

-(IBAction)myButtonsUpInside:(id)sender event:(UIEvent*)event

-(IBAction)myButtonsUpOutside:(id)sender event:(UIEvent*)event

首先检查事件是否应作为不同的按钮处理。像这样:

NSArray* theTouches = [[event allTouches] allObjects];
UITouch* touch = [theTouches objectAtIndex:0];
if([btnMyOtherButton pointInside:[touch locationInView:btnMyOtherButton] withEvent:event])
     //Call myOtherButton's action.
//Otherwise do this button's action.

您也可以使用UIControlEventTouchDragInsideUIControlEventTouchDragOutside 处理程序执行相同的操作来执行类似的操作。我只需要在修饰事件上执行操作即可。

仍然想知道是否有人有其他解决方案来摆脱这个奇怪的缓冲区。

【讨论】: