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];
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...