ios - UIControlEventTouchDragExit 在距 UIButton 100 像素时触发

标签 ios objective-c cocoa-touch drag-and-drop uitouch

目前,UIControlEventTouchDragExit 仅在我将按钮拖离按钮 100 像素时触发。我想自定义此行为并将该范围调整到 25 像素左右,但我对编程还比较陌生,从来不需要重写/自定义这样的内置方法。

我在这里的一些其他帖子中读到我需要子类化 UIButton(或者甚至 UIControl?),并覆盖 -( BOOL) beginTrackingWithTouch: (UITouch *) touch withEvent: (UIEvent *) event 和相关方法,但我真的不知道从哪里开始这样做。

任何人都可以就如何实现这一目标提供一些建议吗?非常感激! ^_^

最佳答案

像这样覆盖 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 - UIControlEventTouchDragExit 在距 UIButton 100 像素时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14340122/

相关文章:

iphone - 带有替换变量的 HTML 的简单 ParseKit 语法

iphone - 删除 UITableViewCell 上的填充

ios - 如何使用 NSURLIsExcludedFromBackupKey 或 kCFURLIsExcludedFromBackupKey?

ios - 旋转图像的碰撞检测

objective-c - 如何在两个 MKCoordinateRegion 之间建立联合

ios - 这个属性声明是什么意思?

iphone - 将 UIWebview 内容转换为 UIImage

iphone - 如何将 UIGestureRecognizer 添加到 UITableViewCell subview ?

c# - 如何编写不同平台的应用程序? Linux、Mac、Windows 和移动平台

ios - 无法将 ZoneConfiguration 转换为 ZoneOptions