【问题标题】:Retrieve and change the point at which touchUpInside changes to touchUpOutside检索并更改 touchUpInside 更改为 touchUpOutside 的点
【发布时间】:2012-06-05 14:49:57
【问题描述】:

我制作了一个UISlider,就像“滑动解锁”滑块一样。 我需要做的是确定抬起手指的点被归类为touchUpOUTSIDE 而不是touchUpINSIDE。这是您将手指滑过滑块末端的位置。 我想这和UIButton 一样,你可以按下按钮然后将手指从按钮上滑开,根据你走多远,它仍然可以归类为touchUpInside。 如果可能的话,我想用圆圈标记目标区域。

一旦我设法找到了这一点,是否可以改变它?所以我可以有更大的目标区域?

我真的不知道从哪里开始。谢谢

【问题讨论】:

    标签: objective-c ios cocoa-touch uibutton uislider


    【解决方案1】:

    根据文档,当手指超出控件边界时会触发UIControlEventTouchUpOutside 事件。如果您尝试更改该区域,滑块将随之缩放。为什么不将UIControlEventTouchUpOutside 的操作与UIControlEventTouchUpInside 绑定?

    【讨论】:

    • 它的面积似乎比界限更大。如果您按下 UIButton 并将手指从按钮上滑开,它会在距离按钮一定距离的地方保持突出显示。这仍然是 inside,但在按钮的范围之外。
    • 系统会大量猜测您手指的实际位置。我们真正可以依赖的只有 Apple 的文档。如果您想自己尝试测试位置,只需跟踪触摸的位置并记录事件发生变化时的坐标即可。
    • 这个问题还说触摸边界更大stackoverflow.com/questions/1300528/…
    【解决方案2】:

    我花了几个小时,但我已经设法解决了这个问题。 我已经做了很多覆盖 touchesMoved、touchesEnded 和 sendAction:action:target:event 的测试,看起来框架类的 70px 内的任何触摸都是触摸 INSIDE。因此,对于 292x52 的 UISlider,从 x:-70 到 x:362 或 y:-70 到 122 的任何触摸都将被视为内部触摸,即使它在框架之外。

    我想出了这段代码,它将覆盖一个自定义类,以允许框架周围 100 像素的更大区域算作内部触摸:

    #import "UICustomSlider.h"
    
    @implementation UICustomSlider {
        BOOL callTouchInside;
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        callTouchInside = NO;
        [super touchesMoved:touches withEvent:event];
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        CGPoint touchLocation = [[touches anyObject] locationInView:self];
        if (touchLocation.x > -100 && touchLocation.x < self.bounds.size.width +100 && touchLocation.y > -100 && touchLocation.y < self.bounds.size.height +100) callTouchInside = YES;
    
        [super touchesEnded:touches withEvent:event];
    }
    
    -(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
    {
        if (action == @selector(sliderTouchOutside)) {                          // This is the selector used for UIControlEventTouchUpOutside
            if (callTouchInside == YES) {
                NSLog(@"Overriding an outside touch to be an inside touch");
                [self sendAction:@selector(UnLockIt) to:target forEvent:event]; // This is the selector used for UIControlEventTouchUpInside
            } else {
                [super sendAction:action to:target forEvent:event];
            }
        } else {
            [super sendAction:action to:target forEvent:event];
        }
    }
    

    稍微调整一下,我应该也可以将它用于相反的情况。 (使用更近的触摸作为外部触摸)。

    【讨论】:

    • 这似乎需要做很多额外的工作。为什么当它真的是外在的时候,你需要触摸是内在的触摸?
    • 因为它很容易将您的手指滑过滚动条的末端并弹回。这样,如果您滑过最后,它仍然会被归类为不幸。
    • 哦,我上面说的外部触摸,实际上是指从内部开始但移到外部的触摸。
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2014-03-05
    • 2021-05-02
    相关资源
    最近更新 更多