使用 UIControlEventTouchDragEnter 触发按钮的方法...但不起作用? [英] Using UIControlEventTouchDragEnter to trigger a button's method... but doesn't work?

查看:15
本文介绍了使用 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 触发按钮的方法...但不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆