0

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

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

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

我怎样才能激活对我的视图控制器中单独存在的图像视图的触摸。

提前致谢..

4

5 回答 5

1

您可以在 touchesBegan: 方法中检查视图中的哪个位置被选中

CGPoint point = [recognizer locationInView:self];

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

for(i = 0; i < 5; i++)
{
    if(CGRectContainsPoint(imageView.frame, point))
    {
        // do something.
    }
}
于 2011-09-12T10:13:43.840 回答
1
UITouch *touch = [touches anyObject];
CGPoint offSetPoint = [touch locationInView:myImageView];

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

于 2011-09-12T10:28:33.107 回答
1
  - (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];

      }

   }
于 2011-09-12T13:29:21.777 回答
0

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

于 2011-09-12T10:12:14.273 回答
0

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

于 2011-09-12T10:19:49.033 回答