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

查看:128
本文介绍了使用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.

根据苹果,此事件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天全站免登陆