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

Using UIControlEventTouchDragEnter to trigger a button#39;s method... but doesn#39;t work?(使用 UIControlEventTouchDragEnter 触发按钮的方法...但不起作用?)
本文介绍了使用 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 触发按钮的方法...但不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)