【发布时间】:2017-01-09 11:21:09
【问题描述】:
我正在使用 cosmicmind 材料 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 的子视图嵌入的菜单。我添加到这个子视图的原因是因为 FAB 位于搜索/显示控制器 (TableView) 的顶部,这样即使滚动表格的内容,FAB 也可以保持在 TableView 的顶部。
当视图最初加载时,我可以单击菜单按钮,动画正确发生并出现 button2。但是,它不允许我点击第二个按钮或通过再次按下 button1 来关闭菜单,除非我导航到选项卡栏控制器中的另一个选项卡,然后导航回 FAB MenuView 所在的选项卡。我正在将我的prepareMenuView() 函数加载到viewDidLoad 中,正如示例中所示。
不确定如何修改它以使其能够按需要运行。选择另一个 ViewController 生命周期方法来运行 prepareMenuView() 是没有意义的。
【问题讨论】:
-
嘿!您介意分享完整的设置代码吗?我将能够从那里提供帮助。谢谢!
-
@CosmicMind 我现在已经添加了我的视图控制器的内容
标签: ios swift cosmicmind material-swift