0

你好,我有 2UIImageViewsUIView

一旦我接触到UIImageView touchesBegan方法就会被调用。但是一旦我拖动UIImageView然后touchesMoved被调用。但同时也称为touchesMoved第二个。UIImageView

你能帮我如何获得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");
}
4

1 回答 1

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

   }
}
于 2012-11-12T09:26:38.443 回答