【问题标题】:How to detect touch end of image view when touches moving触摸移动时如何检测图像视图的触摸结束
【发布时间】:2013-04-06 14:34:44
【问题描述】:

我有一个图像视图。我在这样的图像视图中检测到触摸

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [touches anyObject];
   int viewTag=[touch view].tag;

   if ([[touch view] isKindOfClass:[UIImageView class]])
   {
      //My code
   }
}

并且触摸在图像视图上移动。每当我的触摸在特定时间移出图像视图时,我都需要一个警报视图。如何在触摸移动中从图像视图中检测到触摸?...

【问题讨论】:

    标签: iphone ios uiimageview touches


    【解决方案1】:

    我建议使用UIPanGestureRecognizer 并将其添加到要检测的图像视图的更大超级视图中。这样,即使触摸从外部开始并移入和移出图像视图,您也可以在手势处理程序中跟踪触摸的移动。

    这很简单,例如创建一个名为handlePan: 的方法,使用您的处理程序方法创建手势识别器,将其添加到适当的超级视图中。现在,只要手势处于活动状态并且触摸移动,您的处理程序方法就会被调用,您可以检查它是否在您的图像视图中。

    【讨论】:

      【解决方案2】:

      你应该使用这个方法...

      -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
         UITouch *touch = [touches anyObject];
         int viewTag=[touch view].tag;
      
         if ([[touch view] isKindOfClass:[UIImageView class]])
         {
        //My code
         }
         else
         {
            //show the alertView here
         }
      }
      

      要检查初始点击是否在 imageView 上,您必须在 touchesBegan 方法中设置一个标志...并在 touchesMoved 方法中进行相应检查

      【讨论】:

      • touchesEnded 只在图像中工作,但他想进行 touchesMoved 检测。
      • 改了答案..我想这就是他想要的
      • 感谢您的重播。但它不起作用。在 if 条件下,我放置了 NSLog(@"Touch Moved");在其他条件下 NSLog(@"Touch End");。当我触摸 imageview 并在这个时候触摸在 imageview 上移动时,调用“Touch Moved”日志并在那个时候将触摸移出 imageview 也调用“Touch Moved”
      【解决方案3】:

      您可以在UIImageView上方添加相同大小的透明UIButton并跟踪UIControlEventTouchDragOutside

      [button addTarget:self action:@selector(draggedOutside:) forControlEvents:UIControlEventTouchDragExit];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-16
        • 2019-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多