簡體   English   中英

按下UIImageView時記錄的Touch Up外事件

[英]Touch Up Outside event recorded when pressed a UIImageView

我有一個UIButton,我添加了一個“觸摸外部”操作。 我有一個UIImageView。

我需要以下操作:當用戶觸摸按鈕並在UIImageView處釋放時,我希望執行someAction。

如果用戶觸摸該按鈕並在UIImageView以外的其他視圖中釋放,則不應執行someAction。

這是我的代碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    NSLog(@"Touch x : %f y : %f", location.x, location.y);
    if(CGRectContainsPoint(redBtn.frame, location)){
        NSLog(@"red button pressed");
    }else if (CGRectContainsPoint(blueBtn.frame, location)){
        NSLog(@"blue button pressed");
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{


    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    if(CGRectContainsPoint(firstImageView.frame, location)){
        if(redFlag){
            [firstImageView setBackgroundColor:[UIColor redColor]];
        }
        else if(blueFlag){
            [firstImageView setBackgroundColor:[UIColor blueColor]];
        }

        NSLog(@"touch ended in first view");
    }else if(CGRectContainsPoint(secondImageView.frame, location)){

        if(redFlag){
            [secondImageView setBackgroundColor:[UIColor redColor]];
        }
        else if(blueFlag){
            [secondImageView setBackgroundColor:[UIColor blueColor]];
        }

        NSLog(@"touch ended in second view");
    }

}

使用touchesEnded來實現這一點。 要檢查按鈕是否按下,請使用BOOL,然后在觸摸結束時使用touch.view == imageView然后執行所需的操作。

在按鈕單擊事件上,將BOOL變量設為yes,以確保其被按下。 現在,在touchesEnded的UIResponser中編寫

UITouch *touch = [touches anyObject]; 
if (touch.view == imageView) 
   {
      // do the required thing here
   }

暫無
暫無

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

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