How to cancel a sequence of UITouch events?

前端 未结 12 2485
悲哀的现实
悲哀的现实 2020-12-17 09:43

I have a UIImage view that responds to touch events. I want to cancel the touch sequence, i.e., further calls to touchesMoved:, if the touch goes o

相关标签:
12条回答
  • 2020-12-17 10:14

    This solution may be a bit kludgy, but you could implement and manually call

    - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
    

    I am basing this solution loosely on some tweaking I did to the MoveMe sample app on Apple's iPhone sample code site where I modified the touchesMoved method to look like this:

    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
         UITouch *touch = [touches anyObject];
         if ([touch view] == placardView)
             CGPoint location = [touch locationInView:self];
             placardView.center = location;
             // I added the following line:
             [self touchesCancelled:touches withEvent:event];
             return;
    }
    
    0 讨论(0)
  • 2020-12-17 10:15

    This code will help you. I disable touch event in touchesMoved: method and I am enabling touch event in touchesCancelled: method

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
         UITouch *touch = [touches anyObject];
         .......
         .......
         Your code
         .......
         .......
    
         //Condition when you want to disable touch event
    
         [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
         .......
         .......
         Your code
         .......
         .......
     }
    
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }
    
    0 讨论(0)
  • 2020-12-17 10:17

    I've faced the same problem recently and found a standard way to solve it. You can use [[UIApplication sharedApplication] beginIgnoringInteractionEvents] to stop delivering touchesMoved events to your whole app. Make sure to enable them using [[UIApplication sharedApplication] endIgnoringInteractionEvents] when you need to receive touches again.

    0 讨论(0)
  • 2020-12-17 10:26

    I don't think it's possible because I don't see it documented and none of these solutions work.

    0 讨论(0)
  • 2020-12-17 10:27

    On iOS5 there seems to be a private method in UITouch

    -(void)setSentTouchesEnded:(BOOL)ended;
    

    Depending on apple's implementation it could stop sending events

    Another way of doing so would be using associative objects

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if ([(NSNumber *)objc_getAssociatedObject(touch, &outKey) boolValue])
            return;
        if (sometouchisoutsideofview) {
            objc_setAssociatedObject(touch, &outKey, [NSNumber numberWithBool:YES], OBJC_ASSOCIATION_RETAIN);
        }
    }
    
    0 讨论(0)
  • 2020-12-17 10:29

    You need to call [super touchesMoved:withEvent:] in order to let the super view clean up from the event but more importantly, you need to not call [super touchesCancelled:withEvent:].

    Here's what I used on a cell to keep it from getting selected when I detected a swipe:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (!showingEdit) {
            if (IS_FAR_ENOUGH_TO_BE_A_SWIPE) {
                RESPOND_TO_SWIPE
                showingEdit = YES;
                [super touchesCancelled:touches withEvent:event];
            } else {
                [super touchesMoved:touches withEvent:event];
            }
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if (!showingEdit) {
            [super touchesEnded:touches withEvent:event];
        }
    }
    
    0 讨论(0)
提交回复
热议问题