OGeek|极客世界-中国程序员成长平台

标题: ios - 有谁知道为什么其他代码部分不能隐藏我的颜色选择器? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:05
标题: ios - 有谁知道为什么其他代码部分不能隐藏我的颜色选择器?

我还是 Swift 4 的新手,我在互联网上找到了这个,并试图让这个按钮在图像按钮为 A 时显示“某些东西”。但是当我再次单击按钮时,只有它的图像按钮发生了变化,但是“东西”仍然没有隐藏。有人可以帮忙吗?

我已经完成了使用此动画的其他按钮,但该按钮正在显示库中的另一个按钮。 但是这个 1 不同,没有显示库中的另一个按钮,而是显示 ChromaColorPicker

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

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

============================= 别的地方================= =====

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

我不知道是否 colorPicker.alpha = 0 工作与否



Best Answer-推荐答案


每次调用 configureUI 时,都会向 View 添加一个新的颜色选择器,然后在其上设置 alpha 动画。

因此,当您尝试隐藏 colorPicker 时,实际发生的是您正在添加一个带有 alpha 0 的新颜色选择器并将其动画化为 alpha 0(什么都不做),之前的 colorPicker 仍然可见,所以看起来好像没有任何变化。

colorPicker 创建一次变量并将其添加到 View 中,然后在 configureUI 函数中配置该实例。

关于ios - 有谁知道为什么其他代码部分不能隐藏我的颜色选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54366884/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4