ios – UIControlEventTouchDragExit在距离UIButton 100像素时触发

目前,只有当我从按钮拖出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];
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...