使用UIControlEventTouchDragEnter触发button的方法…但不起作用?

我试图设置一个button使用UIControlEventTouchDragEnter作为触发button的方法的方式。 具体来说,我有一个button,并且我想要button的方法被触发,如果用户在button外按下他们的手指,并将他们的手指拖到button的边界。

根据苹果 ,这个事件,UIControlEventTouchDragEnter,是:一个手指被拖入控件的边界的事件。

但是,我不能得到button来触发。 这是我的代码:

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

所以,当这个button的touchInto被触发时,这个方法会把_sample的当前图像改变成这个替代图像。 如果我只是使用touchUpInside,图像确实会改变为点击button时的替代。

有谁知道为什么这不工作,或有解决办法? 谢谢!

touchDragEnter只有当你最初点击button时触发,将你的手指拖动到button边界的外部,再次拖动到button的边界。

您可能希望在视图控制器类中使用touchesMoved方法,并检测基于触摸位置input的button:

 - (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); }