如何为2个不同的UIImageView调用TouchesMoved

时间:2012-11-12 09:14:16

标签: ios uitouch

您好我UIImageViews中有2 UIView

触摸UIImageView touchesBegan后,方法会被调用。但是,一旦我拖动UIImageView,就会调用touchesMoved。但同时也会调用第touchesMovedUIImageView

请帮助我如何为touchesMoved获取UIImageViews事件?

这是我的代码

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];

    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 Begin");
    if (CGRectContainsPoint(iv2.frame,currentPoint)==YES)
        NSLog(@"iv2 Begin");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    if(CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 Moved NO");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
        NSLog(@"iv1 Moved YES");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==YES)
        NSLog(@"iv2 Moved NO");
    if(CGRectContainsPoint(iv2.frame,currentPoint)==NO)
        NSLog(@"iv2 Moved NO");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];

    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv1 End");
    if (CGRectContainsPoint(iv1.frame,currentPoint)==YES)
        NSLog(@"iv2 End");
}

1 个答案:

答案 0 :(得分:1)

您可以使用链接到两个视图的两个出口,这是用于重新识别触摸方法中的两个视图的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch *t = [touches anyObject];
   touchedView = t.view;
   if (t.view == view1) {

    //todo something with view1;

   } else if (t.view == view2) {

      //todo something with view2

   }
}