UIControlEventTouchDragExit在距离UIButton 100像素时触发 [英] UIControlEventTouchDragExit triggers when 100 pixels away from UIButton
问题描述
目前, UIControlEventTouchDragExit
只有在我从按钮拖动100像素时才会触发。我想自定义这种行为,把这个范围约25个像素,但我是相对较新的编程,从来没有需要重写/自定义一个内置的方法像这样。
At present, the UIControlEventTouchDragExit
only triggers when I drag 100 pixels away from the button. I'd like to customize this behavior and bring that range in to around 25 pixels, but I'm relatively new to programming and have never needed to override / customize an in-built method like this.
我在这里读过一些其他的帖子,我需要继承 UIButton
(或者甚至 UIControl
?)和覆盖 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
真的知道在哪里开始这样做。
I've read in some other posts here that I'd need to subclass the UIButton
(or perhaps even UIControl
?), and override -(BOOL) beginTrackingWithTouch: (UITouch *) touch withEvent: (UIEvent *) event
and related methods, but I don't really know where to begin doing so.
有人可以提出一些建议,如何我可以实现这一点?非常感激! ^ _ ^
Could anyone kindly offer some advice as to how I might achieve this? Much appreciated! ^_^
推荐答案
覆盖continueTrackingWithTouch:withEvent:像这样在默认的gutter中发送DragExit / DragOutside事件:
Override continueTrackingWithTouch:withEvent: like this to send DragExit/DragOutside events inside of the default gutter:
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGFloat boundsExtension = 25.0f;
CGRect outerBounds = CGRectInset(self.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:self]);
if(touchOutside)
{
BOOL previousTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:self]);
if(previousTouchInside)
{
NSLog(@"Sending UIControlEventTouchDragExit");
[self sendActionsForControlEvents:UIControlEventTouchDragExit];
}
else
{
NSLog(@"Sending UIControlEventTouchDragOutside");
[self sendActionsForControlEvents:UIControlEventTouchDragOutside];
}
}
return [super continueTrackingWithTouch:touch withEvent:event];
}
这篇关于UIControlEventTouchDragExit在距离UIButton 100像素时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!