Using UIControlEventTouchDragEnter to trigger a button#39;s method... but doesn#39;t work?(使用 UIControlEventTouchDragEnter 触发按钮的方法...但不起作用?)
问题描述
我正在尝试使用 UIControlEventTouchDragEnter 设置按钮作为触发按钮方法的方式.具体来说,我有一个按钮,如果用户在按钮外按下手指并将手指拖入按钮的边界,我希望触发按钮的方法.
I am trying to set up a button using UIControlEventTouchDragEnter as the way to trigger the button's method. Specifically, I have a button, and I want the button's method to be triggered if the user presses their finger outside of the button, and drags their finger into the bounds of the button.
根据 apple,这个事件,UIControlEventTouchDragEnter,是:手指被拖入控件边界的事件.
According to apple, this event, UIControlEventTouchDragEnter, is: An event where a finger is dragged into the bounds of the control.
但是,我无法触发按钮.这是我的代码:
However, I can't get the button to trigger. Here is my code:
- (IBAction)touchDragEnter:(UIButton *)sender {
_samlpe.image = [UIImage imageNamed:@"alternate_pic.png"];
}
因此,当触发此按钮的 touchInto 时,该方法会将 _sample 的当前图像更改为此备用图像.如果我只使用 touchUpInside,图像会在按钮单击时变为备用图像.
So, when touchInto for this button is triggered, the method will change the current image of _sample into this alternate image. If I just use touchUpInside, the image does change to the alternate upon button click.
有谁知道为什么这不起作用,或者有解决方法吗?谢谢!
Does anyone know why this isn't working, or have work-arounds? Thanks!
推荐答案
touchDragEnter
仅在您最初点击按钮时触发,将手指拖到按钮边界之外,然后再次拖动到按钮的边界.
The touchDragEnter
is only triggered when you initially tap the button, drag your finger to the outside of the bounds of the button, and drag again into the bounds of the button.
您可能希望在视图控制器类中使用 touchesMoved
方法并根据触摸位置检测输入的按钮:
You might want to make use of touchesMoved
method in your view controller class and detect the button entered based on the location of the touch:
- (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);
}
这篇关于使用 UIControlEventTouchDragEnter 触发按钮的方法...但不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UIControlEventTouchDragEnter 触发按钮的方法...但不起作用?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01