0

在 CosmicMind / Material 库中,我如何启动(显示)snackbar?

我试图准备snackbarController,然后在用户单击按钮时显示它。

如他们的示例所示:

private var undoButton: FlatButton!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    prepareSnackbar()
    animateSnackbar()
}

@IBAction func loginBtnTapped(_ sender: AnyObject) {
    sc?.show(vc: UIViewControlle, sender: Any)//doesn't show
    more code... (which works)
}
private func prepareUndoButton() {
    undoButton = FlatButton(title: "Undo", titleColor: Color.yellow.base)
    undoButton.pulseAnimation = .backing
    undoButton.titleLabel?.font = RobotoFont.regular(with: 14)
}

private func prepareSnackbar() {
    guard let sc = snackbarController else {
        return
    }
    sc.snackbar.text = "Reminder saved."
    sc.snackbar.rightViews = [undoButton]

}

private func animateSnackbar() {
    guard let sc = snackbarController else {
        return
    }
    _ = sc.animate(snackbar: .visible, delay: 1)
    _ = sc.animate(snackbar: .hidden, delay: 4)
}

所以我确实在一个空项目上尝试过,但小吃店仍然无法正常工作。你能指出我做错了什么吗?

 import Foundation
 import UIKit
 import Material

class MainViewController: UIViewController {

private var undoButton: FlatButton!


open override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = Color.red.accent1

    prepareUndoButton()
}

open override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //prepareSnackbar()
    //animateSnackbar()
}

private func prepareUndoButton() {
    undoButton = FlatButton(title: "Undo", titleColor: Color.yellow.base)
    undoButton.pulseAnimation = .backing
    undoButton.titleLabel?.font = RobotoFont.regular(with: 14)
}

private func prepareSnackbar() {
    guard let sc = snackbarController else {
        return
    }

    sc.snackbar.text = "Reminder saved."
    sc.snackbar.rightViews = [undoButton]
}

private func animateSnackbar() {
    guard let sc = snackbarController else {
        return
    }

    _ = sc.animate(snackbar: .visible, delay: 1)
    _ = sc.animate(snackbar: .hidden, delay: 4)
}


@IBAction func testBtn(_ sender: AnyObject) {
    print("TEST TEST TEST")

    prepareSnackbar()
    animateSnackbar()
}
}
4

1 回答 1

1

如果您在viewDidAppear函数中注意到您有一个animateSnackbar电话,它会调用线路

_ = sc.animate(snackbar: .visible, delay: 1)
_ = sc.animate(snackbar: .hidden, delay: 4)

您基本上可以animateSnackbar在按钮处理程序中使用该函数,如下所示:

@IBAction func loginBtnTapped(_ sender: AnyObject) {
    animateSnackbar()
}

就是这样。播放了两个动画,一个显示,一个隐藏小吃栏。设置您希望它们自动出现和隐藏的延迟。

您可能希望animateSnackbar从方法中删除调用viewDidAppear,因为该示例已放在那里。

于 2016-10-05T22:39:37.517 回答