0

我在 a 中为 iPhone 创建了一个迷你弹出菜单UIView,我希望用户能够在他们执行任何操作时关闭视图,而不是选择其中一个选项。因此,如果用户点击/滑动/捏住屏幕上的任何其他元素,弹出视图应该会消失。

但是,我不想检测到会阻止其他事情发生的手势......例如,有一个UITableView下面,如果我向上或向下滑动它,我希望它按预期移动消除迷你弹出视图。

我应该使用多个手势识别器,还是应该使用 touchesBegan,或者有更好的方法吗?

4

1 回答 1

2

把这个放在你的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;
}
于 2012-03-22T15:35:39.333 回答