所以我从 github使用这个 Menu.目前,如果再次单击“点击”按钮,菜单会打开并缩回,但如果用户点击除按钮之外的屏幕上的任何其他位置,我还希望菜单缩进.我遇到的问题是我在带有tabbar
以下是它现在如何工作的示例.这是link to the Code.
第一种方法:
您可以为按钮和其他视图设置一个标签,这些视图不希望在敲击菜单时将其关闭:
button.tag=99; button2.tag=99; backgroundImage.tag=99;
然后在viewcontroller中,使用touchesBegan:withEvent:delegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch.view.tag!=99){
//Call your dismiss method
}
}
第二种方法:
如果您的按钮有一个叠加层(例如突出显示按钮的背景,并填满整个视图),您可以为其添加UITapGestureRecognizer,并在每次希望显示自定义视图时将其添加到视图中.这是一个例子:
UIView *overlay;
-(void)addOverlay{
//Add the overlay, if there's one in your code, then you don't have to create this
overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
//Register the tap gesture recognizer
UITapGestureRecognizer *overlayTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(onOverlayTapped)];
[overlay addGestureRecognizer:overlayTap];
[self.view addSubview:overlay];
}
- (void)onOverlayTapped
{
//Call your dismiss method
for (UITapGestureRecognizer *ges in previewOverlay.gestureRecognizers) {
[overlay removeGestureRecognizer:ges];
}
[overlay removeFromSuperview];
}
你可以在这里看到类似案例的my answer.
