UIControlEventTouchDragExit 在距离 UIButton 100 像素时触发 [英] UIControlEventTouchDragExit triggers when 100 pixels away from UIButton

查看:20
本文介绍了UIControlEventTouchDragExit 在距离 UIButton 100 像素时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,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: 以在默认装订线内发送 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆