找出触摸位置在Imageview中

时间:2014-04-22 07:40:30

标签: ios iphone xcode

我尝试运行一些代码,当我的手指移动到imageview所在的相同位置时。

我定义了图片视图的位置:

CGRect imagepos = myImage.frame;

实际触摸位置的位置:

UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];

我试过CGPointEqualToPoint它不起作用。然后我尝试使用CGRectContainsPoint它也没有用,因为当imagepos和currentpos具有相同的x坐标时,我的“一些代码”正在运行(我的手指没有移动到图像,它们只有相同的x位置Y位置完全不同)。

我怎么说,当我的手指(也在方法触摸中移动)触摸/移动到/位于图像的任何位置时,运行一些代码或方法?

感谢回答。

3 个答案:

答案 0 :(得分:1)

在你的touchesBegan方法中,你可以尝试这样的事情:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if([touch view] == myImgView)
    {
        NSLog(@"My Image was touched");
    }
}

这会告诉您何时触摸ImageView。确保在imageView中将userInteractionEnabled设置为YES。

答案 1 :(得分:1)

嘿,请在下面试试 -

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];  //Now you got current location of click or tap

    if(location.y == YOUR_IMAGE_VIEW_POSITION_Y)
    {
      NSLog(@"Got the TOUCH...OOOoopsss !!!");
    }   
  }
}

希望这会有所帮助。

答案 2 :(得分:0)

有一个功能......

CGRectContainsPoint(rect, point);

如果该点位于rect内,则返回bool。

因此,获取触摸点并使用imageView框架运行,以确定触摸是否在图像视图内。