更改alpha时,Swift UIView会闪烁

时间:2016-07-23 09:17:44

标签: ios swift xcode uiview

我在改变UIView的不透明度方面遇到了问题。我这样改变它:UIView.alpha = value。如果我用step = 0.01更改它 - 视图会闪烁。即alpha = 0.05的视图可能比alpha = 0.04的视图更不可见。我用步长= 0.01更改alpha,结果可能是:查看可见性更好 - >更好 - >更差 - >更好 - >更差等等。我不明白为什么会发生这种情况。对于模拟器和真实设备也是如此。该项目:https://drive.google.com/file/d/0B5cO_tAtLPiwcFk2Qk4wbldDT3M/view?usp=sharing 此外,我在20秒内将变化的alpha从0.0变为1.0:youtu.be/in34amFvyOE

import UIKit
class ViewController: UIViewController {
    var alpha = CGFloat()
    override func viewDidLoad() {
        super.viewDidLoad()
        LableA.alpha = 0
        AlphaLabel.text = "Alpha: \(alpha)"
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBOutlet weak var LableA: UILabel!
    @IBOutlet weak var AlphaLabel: UILabel!
    @IBAction func pressedChAlphaBtn(sender: AnyObject) {
        alpha = alpha + 0.01
        LableA.alpha = alpha
        print(alpha)
        AlphaLabel.text = "Alpha: \(alpha)"
    }
    @IBAction func pressedResetBtn(sender: AnyObject) {
        alpha = 0
        LableA.alpha = alpha
        print(alpha)
        AlphaLabel.text = "Alpha: \(alpha)"
    }
}

1 个答案:

答案 0 :(得分:0)

动画更改:

Swift 1,2

UIView.animateWithDuration(0.5) {
    self.LableA.alpha = alpha
}

Swift 3

UIView.animate(withDuration: 0.5) {
    self.LableA.alpha = alpha
}