UI按钮直到第二次按下才执行操作

时间:2017-04-07 05:34:40

标签: ios swift uibutton

我的UITabBar(自定义)和动画上有一个按钮,当按下该按钮时,按钮不会执行两次。我在第一次尝试时使用了print的打印声明,但我不确定为什么它在第一次尝试时不会执行动画。这是我的代码:

func menuButtonAction(sender: UIButton) {
    if sender.currentImage == #imageLiteral(resourceName: "play") {
        UIView.animate(withDuration: 0.3, animations: { 
            sender.setImage(#imageLiteral(resourceName: "playbutton2"), for: .normal)
            self.button2.frame = CGRect(x: self.tabBar.center.x, y: self.tabBar.center.y - 100,  width: self.buttonimage.size.width, height: self.buttonimage.size.height)
            self.view3.alpha = 0.6
        })
    } else {
        sender.setImage(#imageLiteral(resourceName: "play"), for: .normal)
        UIView.animate(withDuration: 0.3, animations: { 
            self.button2.center = self.button.center
            self.view3.alpha = 0
        })
    }

    print("Middle Button was just pressed!")
}

我在viewWillAppear

中通过类似的目标运行此功能
button.addTarget(self, action: #selector(self.menuButtonAction(sender:)), for: .touchUpInside)

1 个答案:

答案 0 :(得分:1)

似乎你没有在xib中的按钮上设置图像,所以在视图中设置你的图像像

那样加载

希望它能运作

button.setImage(#imageLiteral(resourceName: "play"), for: .normal)