ios - 使用 UIControlEventTouchDragEnter 触发按钮的方法......但不起作用?

标签 ios objective-c xcode uibutton uiimage

我正在尝试使用 UIControlEventTouchDragEnter 设置一个按钮作为触发按钮方法的方式。具体来说,我有一个按钮,我希望当用户在按钮外按下手指并将手指拖入按钮边界时触发按钮的方法。

根据 apple ,此事件 UIControlEventTouchDragEnter 是:手指被拖入控件边界的事件。

但是,我无法让按钮触发。这是我的代码:

- (IBAction)touchDragEnter:(UIButton *)sender {
    _samlpe.image = [UIImage imageNamed:@"alternate_pic.png"];
}

因此,当触发此按钮的 touchInto 时,该方法会将 _sample 的当前图像更改为此替代图像。如果我只使用 touchUpInside,单击按钮时图像确实会更改为备用图像。

有谁知道为什么这不起作用,或者有解决方法?谢谢!

最佳答案

touchDragEnter 仅在您最初点击按钮、将手指拖到按钮边界外,然后再次拖入按钮边界时触发。

您可能想在 View Controller 类中使用 touchesMoved 方法并根据触摸的位置检测输入的按钮:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:touch.view];
    NSLog(@"%f - %f", touchLocation.x, touchLocation.y);
}

关于ios - 使用 UIControlEventTouchDragEnter 触发按钮的方法......但不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16661496/

相关文章:

ios - 如何使用日期查找 Nakshatra 和 thithi

iOS:Tapku 日历库 - 允许为当月选择多个日期

iphone - 在 WebView 或浏览器中启动存储的 html 页面 - Objective C/Xcode

swift - 升级到 Xcode 10 后类型检查错误

ios - 渐进式下载和缓存

iPhone - 使用 Sharekit 将图像发送到 Facebook

objective-c - MKAnnotationView - 难以拖动

iphone - 将许多 subview 添加到 View Controller 默认 View 是否会使我的应用程序变慢?

iphone - Objective-C 无法将 NSInteger 分配给 NSInteger 变量?

c++ - 用于检测 arm64 或 armv7 的 xcode 架构宏