动作和图像之间有3个按钮

时间:2016-09-02 11:53:12

标签: ios swift uiimageview uibutton uiimage

我有3个按钮,都有图像和动作。然而,当按下centerButton时,它们都应该改变动作和图像。最简单的方法是什么?

@IBOutlet weak var leftButton: UIButton!
@IBOutlet weak var rightButton: UIButton!
@IBOutlet weak var centerButton: UIButton!


override func draw(_ rect: CGRect) {
    // Drawing code

    centerButton.backgroundColor = UIColor(rgba: "#4834B4")
    centerButton.layer.cornerRadius = self.centerButton.frame.width / 2
    centerButton.addTarget(self, action: #selector(centerButtonClicked), for: UIControlEvents.touchUpInside)

    leftButton.addTarget(self, action: #selector(rightButtonClicked), for: UIControlEvents.touchUpInside)

    rightButton.addTarget(self, action: #selector(rightButtonClicked), for: UIControlEvents.touchUpInside)




}

func centerButtonClicked() {
    if !pictureTaken {
        //change action
    } else {
        //change action
    }



}

1 个答案:

答案 0 :(得分:0)

如果我很清楚你想要这样的东西:

func leftButtonClicked() {
    if centerButton.backgroundColor == UIColor.yellowColor() {
        centerButton.setBackgroundImage(image, forState: .Normal)
    } else {
        rightButton.setBackgroundImage(image, forState: .Normal)
    }

}




func centerButtonClicked() {
   if leftButton.backgroundColor == UIColor.yellowColor() {
        leftButton.setBackgroundImage(image, forState: .Normal)
    } else {
        rightButton.setBackgroundImage(image, forState: .Normal)
    }

我用图像更改写了两个动作。

相关问题