在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