Retrieve and change the point at which touchUpInsi

2019-07-28 17:36发布

I have made a UISlider work just like the "slide to unlock" slider. What I need to do is determine the point at which lifting your finger off is classed as touchUpOUTSIDE and not touchUpINSIDE. This is the point where you slide your finger past the end of the slider too far. I guess it's the same as with a UIButton, you can press the button then slide your finger off the button and depending how far you go, it can still be classed as touchUpInside. If possible, i'd like to mark the target area with a circle.

Once i've managed to find where this point is, is it possible to change it? So I could have a bigger target area?

I really don't know where to start with this. Thanks

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-28 17:53

It's taken me a few hours, but i've managed to sort this. I've done a lot of testing overriding touchesMoved, touchesEnded and sendAction:action:target:event and it seems that any touch within 70px of the frame classes as a touch INSIDE. So for a UISlider that's 292x52 any touch from x:-70 to x:362 or y:-70 to 122 will count as an inside touch, even though it's outside the frame.

I have come up with this code that will override a custom class to allow a bigger area of 100px around the frame to count as an inside touch:

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

With a little bit more tweaking I should be able to use it for the opposite also. (Using a closer touch as an outside touch).

查看更多
三岁会撩人
3楼-- · 2019-07-28 18:04

According to the docs, the UIControlEventTouchUpOutside event is triggered when the finger is outside the bounds of the control. If you're trying to change that area, the slider will scale with it. Why not just tie the action for UIControlEventTouchUpOutside to the same as UIControlEventTouchUpInside?

查看更多
登录 后发表回答