How to cancel a sequence of UITouch events?

前端 未结 12 2522
悲哀的现实
悲哀的现实 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条回答
  •  -上瘾入骨i
    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];
         }
     }
    

提交回复
热议问题