目前,只有当我从按钮拖出100像素时,才会触发UIControlEventTouchDragExit.我想自定义这种行为并将该范围带到大约25像素,但我对编程相对较新,并且从未需要覆盖/自定义这样的内置方法.
我在这里读了一些其他帖子,我需要继承UIButton(或者甚至是UIControl?),并覆盖 – (BOOL)beginTrackingWithTouch:(UITouch *)触及withEvent:(UIEvent *)事件和相关方法,但我真的不知道从哪里开始这样做.
有谁可以提供一些关于如何实现这一目标的建议?非常感激! ^ _ ^
解决方法
覆盖continueTrackingWithTouch:withEvent:像这样在默认装订线内发送DragExit / DragOutside事件:
- (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]; }