2

I'm still new with Swift 4 and i found this on the internet & tried to make this Button to show "something" when the image button is A. But when i clicked the Button again, only its image button changed but the "something" still not hidden. Can someone help ?

I already done with other button that using this animation but the button is showing another button from Library. But this 1 is different, not showing another button from Library but showing ChromaColorPicker

var sizeOff = UIImage(named: "Brush-Size")

var sizeOn = UIImage(named: "Brush-Size-On")

============================ somewhere else ======================

extension ViewController {

    @IBAction func BrushColourClicked(_ sender: UIButton) {
        if sender.currentImage == ColourOn {
            sender.setImage(ColourOff, for: .normal)
        } else {
            sender.setImage(ColourOn, for: .normal)
        } // Image first set
        configureUI()

    }

    func configureUI() {
        let colorPicker = ChromaColorPicker(frame: CGRect(x: 25.0, y: 410.0, width: 140.0, height: 140.0)) // Position & Size of Color Picker

        ColourButtonCenter = colorPicker.center
        colorPicker.center = BrushColour.center

        colorPicker.delegate = self
        colorPicker.padding = 5.0
        colorPicker.stroke = 3.0
        colorPicker.hexLabel.isHidden = true
        colorPicker.layout()
        view.addSubview(colorPicker)
        colorPicker.alpha = 0

        if BrushColour.currentImage == ColourOn {
            UIView.animate(withDuration: 0.35, animations: {
                colorPicker.alpha = 1
                colorPicker.center = self.ColourButtonCenter
            })
        } // Animation show
        else {
            UIView.animate(withDuration: 0.35, animations: {
                colorPicker.alpha = 0
                colorPicker.center = self.BrushColour.center
            })
        } // Animation hide
    }
}

i dont know if colorPicker.alpha = 0 is working or not

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
BasDSasr
  • 31
  • 2
  • 2
    You shouldn't hide UI elements using their `alpha` property, you should use the `isHidden` property. Unrelated to your question, but you should also conform to the Swift naming convention, which is lowerCamelCase for variable and function names (`colourButtonCenter`, `brushColourClicked`, `colourOn`, `colourOff`). – Dávid Pásztor Jan 25 '19 at 14:15
  • @DávidPásztor There is nothing wrong with using `alpha` instead of `isHidden`. View with `alpha = 0` behaves the same as view with `isHidden = false`. – Sulthan Jan 25 '19 at 15:45
  • still nothing happen after changing to isHidden, i'll try to find the real problem :o – BasDSasr Jan 28 '19 at 10:56

1 Answers1

0

Every time you call configureUI you're adding a new colour picker to the view, then animating the alpha on it.

So when you're trying to hide the colorPicker what's actually happening is that you're adding a new colour picker with alpha 0 and animating it to alpha 0 (doing nothing), the previous colorPicker will still be visible so it will look as though nothing as changed.

Create a variable for colorPicker once and add it to the view, then configure that instance in your configureUI function.

James P
  • 4,786
  • 2
  • 35
  • 52