【问题标题】:cosmicmind material swift MenuView not closingcosmicmind材料swift MenuView没有关闭
【发布时间】: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


【解决方案1】:

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

  1. handleMenu 调用添加到handleButton

internal func handleButton(button: UIButton) { print("Hit Button \(button)") handleMenu(button) }

  1. handleMenubutton2 实例添加一个选择器处理程序。

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

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

:) 一切顺利!

【讨论】:

  • 只有当我移动到另一个选项卡并导航回第一个选项卡时,这仍然有效。击中 FAB 后,我必须再次触发 viewDidLoad() 才能关闭按钮 2 甚至点击按钮 2。在我离开并再次返回之前,按钮 2 上不会显示脉冲动画。
  • 你是如何添加这个视图控制器的?当我以编程方式将视图控制器添加到 appDelegate 的 window.rootViewController 属性时,这对我来说非常有效。您正在描述标签...您的标签的代码在哪里?
猜你喜欢
  • 2016-05-24
  • 1970-01-01
  • 2020-09-10
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2016-04-02
相关资源
最近更新 更多