簡體   English   中英

如何為2個不同的UIImageView調用TouchesMoved

[英]How to called TouchesMoved for 2 different UIImageView

您好,我在UIView有2個UIImageViews

一旦我觸摸UIImageView touchesBegan就會調用touchesBegan方法。 但是,一旦我在UIImageView拖動,就會調用touchesMoved 但同時第二個UIImageView touchesMoved也被調用。

您能幫我兩個UIImageViews如何獲取touchesMoved事件嗎?

這是我的代碼

-(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");
}

您可以使用鏈接到兩個視圖的兩個插座,這是用於在觸摸方法中重新識別兩個視圖的代碼:

-(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

   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM