检索并更改 touchUpInside 更改为 touchUpOutside



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

一旦我设法找到这个点在哪里,是否有可能改变它?所以我可以有更大的目标区域吗?

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

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

我花了几个小时,但我设法对此进行了排序。我已经做了很多测试覆盖触摸移动,触摸结束和发送操作:动作:目标:事件,似乎框架类70px内的任何触摸都作为触摸内部。因此,对于 292x52 的 UISlider,从 x:-70 到 x:362 或 y:-70 到 122 的任何触摸都将算作内部触摸,即使它在框架外。

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

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

通过更多的调整,我也应该能够将其用于相反的情况。(使用更近的触摸作为外部触摸)。

最新更新