【发布时间】:2017-02-20 06:24:54
【问题描述】:
我有一个UIViewController,其中包含一个渲染波形的子视图。当按下按钮时,我试图让主UIView、子视图和导航栏从白色变为灰色。我已经能够改变主视图和导航栏,但不知道如何让子视图改变波形。现在子视图立即变为灰色,而不是与其他视图和导航栏同步动画。
//View Controller Code
UIView.animate(withDuration: 10.0, animations: {
self.navigationController?.navigationBar.barTintColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.navigationController?.navigationBar.layoutIfNeeded()
self.waveform.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.view.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
}, completion: { finished in
//do stuff
})
//waveform view
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
override func draw(_ rect: CGRect) {
for i in 0..<self.circles.count {
self.draw(self.circles[i], ratio: -1.0)
self.draw(self.circles[i], ratio: 1.0)
}
}
func update(_ level: Double) {
amplitude = fmin(normalizedPowerLevelFromDecibels(level), maxAmplitude)
setNeedsDisplay()
}
//更新动画
UIView.transition(with: self.view, duration: 1.0, options: [.transitionCrossDissolve], animations: {
self.view.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.navigationController?.navigationBar.barTintColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.navigationController?.navigationBar.layoutIfNeeded()
self.waveform.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
}, completion: nil)
这会导致波形视图和导航栏同步动画,但self.view 在其他两个视图完成动画后立即变为深灰色...
【问题讨论】:
-
无法从上面的代码块中得到一个想法,请您与 arunjos007@gmail.com 分享您的项目,然后我会尝试找出问题
-
朋友你在设置初始颜色吗?
-
我已经尝试过您的代码,它运行良好。所有三种颜色都在动画中......
-
@Mukesh 在尝试将其设置为灰色之前,我不会在任何地方设置背景颜色。它在故事板中的默认白色
-
我已经更新了答案,那段代码总是为我工作。
标签: ios swift uiview uianimation