【问题标题】:uiimageview touches problemuiimageview 碰到问题
【发布时间】:2011-09-12 10:05:38
【问题描述】:

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

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

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

如何激活对视图控制器中单独存在的图像视图的触摸。

提前谢谢..

【问题讨论】:

    标签: iphone objective-c ipad touches


    【解决方案1】:

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

    CGPoint point = [recognizer locationInView:self];
    

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

    for(i = 0; i < 5; i++)
    {
        if(CGRectContainsPoint(imageView.frame, point))
        {
            // do something.
        }
    }
    

    【讨论】:

      【解决方案2】:
      UITouch *touch = [touches anyObject];
      CGPoint offSetPoint = [touch locationInView:myImageView];
      

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

      【讨论】:

      • 您需要检查触摸的坐标范围才能仅在图像视图上检测到
      【解决方案3】:
        - (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];
      
            }
      
         }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 谢谢,但它不适用于整个视图。我只想在 imageview 上
        【解决方案5】:

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

        【讨论】:

        • 我的游戏需要视图和小图像视图
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-13
        • 2010-11-30
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        • 2015-05-03
        • 2011-05-31
        相关资源
        最近更新 更多