繁体   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