在UIViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view!=yourView && yourView) {
[yourView removeFromSuperview];
yourView=nil;
}
}
编辑将这个:以检测触摸且仅当视图是否存在删除变化
EDIT2:你可以添加以下到您的UIButtons/UITableView
方法
if (yourView) {
[yourView removeFromSuperview];
yourView=nil;
}
或将touchesBegan:withEvent:
作为touchDown事件添加到您的按钮。
这两个恼人的事做,但看不到另一种方式做到这一点,因为touchesBegan
方法不会被交互式元素调用。
EDIT3:对废钢的是,想我已经把它钉
在你的界面添加UIGestureRecognizerDelegate
@interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
然后在viewDidLoad
添加此
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];
然后在您的viewController添加这两种方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view!=yourView && yourView) {
return YES;
}
return NO;
}
-(void)tapMethod {
[yourView removeFromSuperview];
yourView=nil;
}
看到这篇文章http://stackoverflow.com/questions/6078001/how-do-you-detect-touches-in-specific-uiview-when-sliding-finger-across-mutiple – rakeshNS 2012-03-22 17:28:45