Retrieve and change the point at which touchUpInside changes to touchUpOutside

末鹿安然 提交于 2020-01-05 20:21:05

问题


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


回答1:


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?




回答2:


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).



来源:https://stackoverflow.com/questions/10899552/retrieve-and-change-the-point-at-which-touchupinside-changes-to-touchupoutside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!