Peter Pik Peter Pik - 3 months ago 7
iOS Question

3 buttons change between actions and images

i have 3 buttons that all have an image and action. However when the centerButton is pressed they should all change action and image. What is the easiest way to do this?

@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
}



}

func leftButtonClicked() {

if !pictureTaken {
//change action
} else {
//change action
}

}

func rightButtonClicked() {

if !pictureTaken {
//change action
} else {
//change action
}

}

Answer

If I have known well you want something like this:

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)
    }

I write two actions with an image changement.

Comments