繁体   English   中英

uiimageview触及问题

[英]uiimageview touches problem

我正在开发需要uiimageviews的纸牌游戏。 我有一个基于视图的应用程序。 我在其中添加了5imageviews。

每个imageview都有一个唯一的卡片。 现在,用户可以将一张卡片拖到其他存在的卡片上。 为此,我需要知道选择了哪个卡。 这个怎么做?

在我看来,启用触摸功能可以在所有地方使用。 我希望仅在uiimageview上启用触摸。

我该如何激活对ViewController中仅存在的imageviews的触摸。

提前致谢..

您可以做的是touchesBegan:方法,您可以使用以下方法检查视图中的哪个位置

CGPoint point = [recognizer locationInView:self];

然后执行for循环来检查像这样选择了哪个imageview

for(i = 0; i < 5; i++)
{
    if(CGRectContainsPoint(imageView.frame, point))
    {
        // do something.
    }
}
UITouch *touch = [touches anyObject];
CGPoint offSetPoint = [touch locationInView:myImageView];

现在,您仅定义了myImageView对象来接收触摸。 根据您的需要,将这些行编写为touchesBegan或touchesEnded方法...希望对您有所帮助。...不要忘记将imageview的userInteractionEnabled属性设置为YES,以便进行触摸。

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
  {
     CGPoint pnt = [[touches anyObject]locationInView:self.view];
         UIImageView *imgView;
      if((pnt.x > CGRectGetMinX(imgView.frame) && pnt.x < CGRectGetMaxX(imgView.frame))&& 
            (pnt.y > CGRectGetMinY(imgView.frame) && pnt.y < CGRectGetMaxY(imgView.frame)) && 
             ChkOfferCount==TRUE)
      {
    UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Image Touched" 
                               message:@"Selected image is this" delegate:nil  
                               cancelButtonTitle:@"cancel" 
                               otherButtonTitles:@"ok",nil];
            [obj show];
            [obj release];

      }

   }

设置imageView.userInteractionEnabled=YES; 您可以使用imageView本身的touches方法,例如touchesBegan:您可以使用这些方法来处理卡片的拖放。

您还可以创建UIControl视图,然后用UIImageView完全填充它,然后仅捕获UIControl中的触摸事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM