gpt4 book ai didi

ios - cosmicmind Material swift MenuView 不关闭

转载 作者:行者123 更新时间:2023-11-28 06:35:14 27 4
gpt4 key购买 nike

我正在使用 cosmicmind material swift 库,并按照示例代码尝试让 FAB MenuView 正常工作。

我已经复制了代码并添加了我想要的按钮,为了测试我只是用 2 个按钮进行测试。我面临的问题是 handleMenu 函数:

/// Handle the menuView touch event.
internal func handleMenu() {
if menuView.menu.opened {
menuView.close()
(menuView.menu.views?.first as? MaterialButton)?.animate(MaterialAnimation.rotate(rotation: 0))
} else {
menuView.menu.open() { (v: UIView) in
(v as? MaterialButton)?.pulse()
}
(menuView.menu.views?.first as? MaterialButton)?.animate(MaterialAnimation.rotate(rotation: 0.125))
}
}

此 UINavigationController 的完整代码:

import UIKit
import Material

class MyTeeUpsController: UINavigationController {
/// MenuView reference.
private lazy var menuView: MenuView = MenuView()

/// Default spacing size
let spacing: CGFloat = 16

/// Diameter for FabButtons.
let diameter: CGFloat = 56

/// Handle the menuView touch event.
internal func handleMenu() {
if menuView.menu.opened {
menuView.close()
(menuView.menu.views?.first as? MaterialButton)?.animate(MaterialAnimation.rotate(rotation: 0))
} else {
menuView.menu.open() { (v: UIView) in
(v as? MaterialButton)?.pulse()
}
(menuView.menu.views?.first as? MaterialButton)?.animate(MaterialAnimation.rotate(rotation: 0.125))
}
}

/// Handle the menuView touch event.
internal func handleButton(button: UIButton) {
print("Hit Button \(button)")
}


private func prepareMenuView() {
//let w: CGFloat = 52
var img:UIImage? = MaterialIcon.cm.add?.imageWithRenderingMode(.AlwaysTemplate)
let button1: FabButton = FabButton()//frame: CGRectMake((view.bounds.width - w)-10, 550,w,w))
button1.setImage(img, forState: .Normal)
button1.setImage(img, forState: .Highlighted)
button1.pulseColor = MaterialColor.blue.accent3
button1.backgroundColor = MaterialColor.blueGrey.lighten1
button1.borderColor = MaterialColor.blue.accent3
button1.borderWidth = 1
button1.addTarget(self, action: #selector(handleMenu), forControlEvents: .TouchUpInside)
menuView.addSubview(button1)


img = UIImage(named: "filing_cabinet")?.imageWithRenderingMode(.AlwaysTemplate)
let button2:FabButton = FabButton()
button2.depth = .None
button2.setImage(img, forState: .Normal)
button2.setImage(img, forState: .Highlighted)
button2.pulseColor = MaterialColor.blue.accent3
button2.borderColor = MaterialColor.blue.accent3
button2.borderWidth = 1
button2.backgroundColor = MaterialColor.blueGrey.lighten1
button2.addTarget(self, action: #selector(handleButton), forControlEvents: .TouchUpInside)
menuView.addSubview(button2)


menuView.menu.direction = .Up
menuView.menu.baseSize = CGSizeMake(diameter, diameter)
menuView.menu.views = [button1,button2]
view.layout(menuView).width(diameter).height(diameter).bottomRight(bottom: 58, right: 20)
}
private func prepareTabBarItem() {

//todo
}
override func viewDidLoad() {
super.viewDidLoad()
prepareMenuView()
}
}

我作为 UINavigationController 的 subview 嵌入的菜单。我添加到这个 subview 的原因是因为 FAB 位于搜索/显示 Controller (TableView) 之上,这样即使滚动表格的内容,FAB 也可以保持在 TableView 之上。

当 View 最初加载时,我可以单击菜单按钮,动画正确发生并出现 button2。但是,它不允许我点击第二个按钮或再次按下 button1 关闭菜单,除非我导航到选项卡栏 Controller 中的另一个选项卡,然后导航回 FAB MenuView 所在的选项卡。我在 viewDidLoad 中加载我的 prepareMenuView() 函数,正如示例中所示。

不确定如何修改它以便它可以按预期运行。选择另一个 ViewController 生命周期方法来运行 prepareMenuView() 没有意义。

最佳答案

所以您的代码的问题是 button2 只有 handleButton 的选择器处理程序。 handleMenu 处理程序未添加到其中。所以你有两个解决方案。

  1. handleMenu 调用添加到 handleButton


内部函数句柄按钮(按钮:UIButton){
打印(“点击按钮\(按钮)”)
handleMenu(按钮)
}

  1. 将选择器处理程序添加到 handleMenubutton2 实例。


button2.addTarget( self , Action :#selector(handleMenu),forControlEvents:.TouchUpInside)
button2.addTarget( self , Action :#selector(handleButton),forControlEvents:.TouchUpInside)

任一选项都可以,请记住顺序很重要。因此,如果您希望在加载某些内容之前关闭菜单,请在添加 handleButton 之前调用方法或添加选择器处理程序 handleMenu

:) 一切顺利!

关于ios - cosmicmind Material swift MenuView 不关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39273816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com