mercredi 15 juin 2016

change background color of custom UIButton (CoreGraphics) when button is pressed

Does someone know how to change the background color of a custom UIButton?

enter image description here

As you can see in the image the button named plot has a background color of orange. But the button left to it(the black triangle) is not in orange.

What I want is when I press button "plot" or "award" the triangle button next to it also change with it.

Here is my code:

@IBDesignable class LeadingButton: UIButton {

override func drawRect(rect: CGRect) {

    // *Draw triangle button:

    // Create context for triangle
    let context = UIGraphicsGetCurrentContext()

    // *Put information into context:

    // Info for drawing triangle:
    CGContextMoveToPoint(context, 17, 0)
    CGContextAddLineToPoint(context, 0, 40)
    CGContextAddLineToPoint(context, 17, 40)
    CGContextAddLineToPoint(context, 17, 0)

    // Info color triangle
    CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)

    // Color in triangle with specified color
    CGContextFillPath(context)

    // Draw triangle
    CGContextStrokePath(context)
}
}

class MovieDetailController: UIViewController {

 override func viewDidLoad() {
        super.viewDidLoad()
   // For changing background color of buttons
        self.buttonArray = [plotB, directorB, writerB, actorsB, countryB, awardB, posterB, leadingB, trailingB]

    }

    func isButton(pressed: Int) {

        switch pressed {
        case 0...8:

            let selectedButton = buttonArray[pressed]
            selectedButton.backgroundColor = UIColor.orangeColor()

            var idDictionary = [0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6]
            idDictionary.removeValueForKey(pressed)

            for (_, value) in idDictionary {

                if value == 6 {

                }
                else {

                    let remainButtons = buttonArray[value]
                    remainButtons.backgroundColor = UIColor.blackColor()
                }
            }

        default:
            print("test")
        }

    }

Aucun commentaire:

Enregistrer un commentaire