检索并更改touchUpInside更改为touchUpOutside的点

时间:2012-06-05 14:49:57

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

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

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这花了我几个小时,但我已经设法对此进行排序。 我已经做了很多测试覆盖touchesMoved,touchesEnded和sendAction:action:target:event并且看起来任何触摸都在70px的帧类中作为触摸INSIDE。所以对于一个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];
    }
}

稍微调整一下,我也应该能够使用它。 (使用更接近的触摸作为外部触摸)。

相关问题