So I'm using this Menu from github. Currently the menu opens on tap and retracts if you click the tap button again, but I also want the menu to retract if the user taps anywhere else on the screen except the buttons. The problem I'm running into is that I am implementing this in navbar with tabbarcontroller, what happens is that if i tap the button to open the menu and then click on the different tab without collapsing the bubble menu. And then if I come back to the same tab, the bubble menu is visually still open but in code it still thinks it is collapsed, that leads to weird behavior of adding another subview any suggestions?
所以我在github上使用这个菜单。如果再次单击“点击”按钮,当前菜单会打开并缩回,但如果用户点击屏幕上除按钮之外的任何其他位置,我还希望菜单缩进。我遇到的问题是我在带有tabbarcontroller的导航栏中实现这个问题,如果我点击按钮打开菜单然后点击不同的选项卡而不折叠气泡菜单会发生什么。然后,如果我回到相同的选项卡,泡泡菜单在视觉上仍然打开,但在代码中它仍然认为它已折叠,这导致添加另一个子视图任何建议的奇怪行为?
The following is the example of how it works now. And here is the link to the Code.
以下是它现在如何工作的示例。这是该守则的链接。
3 个解决方案
#1
5
There are 2 approaches:
有两种方法:
First approach:
You can set a tag for your buttons and other views that you don't want the menu to be dismissed when they get tapped:
您可以为按钮和其他视图设置一个标签,这些视图不希望在敲击菜单时将其关闭:
button.tag=99;
button2.tag=99;
backgroundImage.tag=99;
And then in your viewcontroller, use the touchesBegan:withEvent:
delegate
然后在viewcontroller中,使用touchesBegan:withEvent:delegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch.view.tag!=99){
//Call your dismiss method
}
}
Second approach:
If there's an overlay for your buttons (e.g. the background to highlight your buttons, and fill all over your view) , you can add an UITapGestureRecognizer
to it, and add it to your view every time you want your custom view to show up. Here's an example:
如果您的按钮有一个叠加层(例如,突出显示按钮的背景,并填充整个视图),您可以为其添加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];
}
You can see my answer here for a similar case.
对于类似的案例,你可以在这里看到我的答案。
#2
1
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
//retract menu
}
This function detects a background tap.
此功能可检测背景点按。
#3
1
I used FlysoFast code and converted it to Swift, to detect an outside tap:
我使用FlysoFast代码并将其转换为Swift,以检测外部点击:
self.overlay = UIView(frame: UIScreen.mainScreen().bounds)
self.overlay.backgroundColor = UIColor.clearColor()
downMenuButton.delegate = self
let tap = UITapGestureRecognizer(target: self, action: Selector("bubbleMenuButtonShouldCollapse"))
tap.numberOfTapsRequired = 1
self.overlay.addGestureRecognizer(tap)
self.overlay.addSubview(downMenuButton)
self.navigationController!.view.addSubview(overlay)
Then I used the following code:
然后我使用以下代码:
func bubbleMenuButtonShouldCollapse() {
println("Overlay tapped")
self.downMenuButton.dismissButtons()
}
to collapse the menu.
折叠菜单。
#1
5
There are 2 approaches:
有两种方法:
First approach:
You can set a tag for your buttons and other views that you don't want the menu to be dismissed when they get tapped:
您可以为按钮和其他视图设置一个标签,这些视图不希望在敲击菜单时将其关闭:
button.tag=99;
button2.tag=99;
backgroundImage.tag=99;
And then in your viewcontroller, use the touchesBegan:withEvent:
delegate
然后在viewcontroller中,使用touchesBegan:withEvent:delegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch.view.tag!=99){
//Call your dismiss method
}
}
Second approach:
If there's an overlay for your buttons (e.g. the background to highlight your buttons, and fill all over your view) , you can add an UITapGestureRecognizer
to it, and add it to your view every time you want your custom view to show up. Here's an example:
如果您的按钮有一个叠加层(例如,突出显示按钮的背景,并填充整个视图),您可以为其添加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];
}
You can see my answer here for a similar case.
对于类似的案例,你可以在这里看到我的答案。
#2
1
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
//retract menu
}
This function detects a background tap.
此功能可检测背景点按。
#3
1
I used FlysoFast code and converted it to Swift, to detect an outside tap:
我使用FlysoFast代码并将其转换为Swift,以检测外部点击:
self.overlay = UIView(frame: UIScreen.mainScreen().bounds)
self.overlay.backgroundColor = UIColor.clearColor()
downMenuButton.delegate = self
let tap = UITapGestureRecognizer(target: self, action: Selector("bubbleMenuButtonShouldCollapse"))
tap.numberOfTapsRequired = 1
self.overlay.addGestureRecognizer(tap)
self.overlay.addSubview(downMenuButton)
self.navigationController!.view.addSubview(overlay)
Then I used the following code:
然后我使用以下代码:
func bubbleMenuButtonShouldCollapse() {
println("Overlay tapped")
self.downMenuButton.dismissButtons()
}
to collapse the menu.
折叠菜单。