How to cancel a sequence of UITouch events?

前端 未结 12 2486
悲哀的现实
悲哀的现实 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:29

    Try temporary setting the UIImageView's userInteractionEnabled property to NO

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

    I achieve this by removing the view from its superview and adding it straight back.

    [view retain];
    UIView *sv = view.superview;
    [view removeFromSuperview];
    [sv addSubview:view];
    [view release];
    

    This breaks the responder chain to the view so any remaining touches will not be received on the view. The next touch will still be received as normal.

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

    You can check that, the touch point locations are in CGRect (ie. points are in Rectangle of your imageview) or not. If they are not in that Rect, the touch will be cancelled.

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
     {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    
        if (CGRectContainsPoint(myImageView, touchLocation))
        {
           lastPoint = [touch locationInView: myImageView];
        }
    
        NSLog(@"Last :%.2f - %.2f",lastPoint.x, lastPoint.y);   
      }
    
     - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
     {
      CGPoint currentPoint;
      UITouch *touch = [touches anyObject];
      CGPoint touchLocation = [touch locationInView:self.view];
    
         if (CGRectContainsPoint(myImageView.frame, touchLocation))
         {
           currentPoint = [touch locationInView: myImageView];
         }
     }
    
    0 讨论(0)
  • 2020-12-17 10:36

    You may create category to UITouch. You can to declare

    @property (nonatomic,strong,readwrite) UIView *view;
    

    And when you change touch.view to nil, for example, you can simulate end of dispatching touch events

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

    "Brute force" solution depending if it suits your needs: Remove and re-initialize the subview that receives the touches or responds to them (of course take care of frame and content).

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

    I was just trying to solve something like this, and found that none of the solutions listed her worked for me. The best i could manage was temporarily ignoring the touches, but then they resumed when the touch re-entered the view.

    Finally solved it.

    1. Set up a boolean flag called "disabled"
    2. In touchedMoved, first check that the touch is inside the desired view. If it isn't, set the disabled flag to YES.
    3. Still in in touchedMoved, check the flag before performing your action. So, at this point, nothing will happen if they're outside the view.
    4. In touchesEnded, set the disabled flag to NO. So the touch sequence is effectively halted until user lifts their finger.

    I guess this will work for you unless you have a specific reason for literally needing the touch sequence cancelled.

    0 讨论(0)
提交回复
热议问题