UI 按钮在您第二次按下之前不会执行操作



我的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)

似乎您没有在 xib 中设置图像按钮,因此在视图中设置图像确实加载了

希望它会起作用

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

最新更新