【问题标题】:How to get which object began touch in iphone如何在iphone中获取哪个对象开始触摸
【发布时间】:2012-07-25 11:06:28
【问题描述】:

我在 iPhone 应用程序中有一个视图。在那个视图中,我添加了两个 UIImageView,比如说 img_view1、img_view2。现在我将触摸事件放在该视图上,如下所示。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     // Some code are here
}

如何获取开始触摸的图像视图?

【问题讨论】:

    标签: iphone uiimageview uitouch


    【解决方案1】:
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];
    
    if (CGRectContainsPoint(img_view1.frame, pt)
    {
        NSLog(@"Image View 1 touched");
    }
    

    等等

    【讨论】:

    • 错误:语义问题:将“UIImageView *”传递给不兼容类型“CGRect”(又名“struct CGRect”)的参数
    【解决方案2】:

    在 xib 中启用您的图像视图用户交互(或者如果您以编程方式添加它,则通过代码)并使用 UIResponder 方法 touchesBegan、touchesMoved、touchesEnded 等来检测图像视图上的触摸:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    
    if ([touch view] == yourImageView)
    {
            //add your code for image touch here 
    }
    

    }

    【讨论】:

      【解决方案3】:

      使用此代码

      UITouch *touch = [touches anyObject];
      
      if (touch.view==imageView1) { 
        // do something
      }
      else if (touch.view==imageView2) { 
        // do something else
      }
      

      希望这就是你要找的。​​p>

      享受编码:)

      【讨论】:

        【解决方案4】:

        使用此代码:

        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
        {
        
           UITouch *touch = [touches anyObject];
                if(touch.view == img_view1)
                {
            }
            else if(touch.view == img_view2)
                {
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-05
          • 1970-01-01
          相关资源
          最近更新 更多