关闭动作按钮项上的动作按钮,单击Swift 4.2

时间:2019-07-04 14:41:10

标签: ios swift

我正在按照this教程创建操作按钮。 Here是整个项目代码。

var actionButton : ActionButton!
override func viewDidLoad() {
    super.viewDidLoad()
    setupButtons()
}

func setupButtons(){
    let google = ActionButtonItem(title: "Google", image: #imageLiteral(resourceName: "a1"))
    google.action = { item in self.view.backgroundColor = UIColor.red }
    let twitter = ActionButtonItem(title: "Twitter", image: #imageLiteral(resourceName: "a2"))
    twitter.action = { item in self.view.backgroundColor = UIColor.blue }
    let facebook = ActionButtonItem(title: "Facebook", image: #imageLiteral(resourceName: "a3"))
    let linkedin = ActionButtonItem(title: "Linkedin", image: #imageLiteral(resourceName: "a4"))
    actionButton = ActionButton(attachedToView: self.view, items: [google, twitter, facebook, linkedin])
    actionButton.setTitle("+", forState: UIControl.State())
    actionButton.backgroundColor = UIColor(red: 238.0/255.0, green: 130.0/255.0, blue: 130.0/255.0, alpha: 1)
    actionButton.action = { button in button.toggleMenu()}
}

该代码运行正常,但是当我单击操作按钮项时,例如说Google或Twitter,我希望关闭操作按钮。

1 个答案:

答案 0 :(得分:1)

为每个self.actionButton.toggleMenu()添加button.action

示例:

google.action = {
        item in self.view.backgroundColor = UIColor.red
        self.actionButton.toggleMenu()
    }