i具有一个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
立即更改为深灰色...
我做动画的想法就像这样
Objective-C
//Set Initial frame or color here & then write block for animation
[UIView animateWithDuration:1.0f animations:^{
// Now set your changing animation frame or color here
}completion:^(BOOL finished) {
//Do completion stuff here
}];
如果您要通过anyView.layer.backgroundColor
设置它,则#import <QuartzCore/QuartzCore.h>
swift
//Set Initial frame or color here & then write block for animation
UIView.animate(withDuration: 5.0, animations: {
// Now set your changing animation frame or color here
self.view.layer.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00).cgColor
}, completion: { finished in
//do completion stuffs
})
如果您要通过anyView.layer.backgroundColor
设置它,则import QuartzCore
尝试以下操作: -
UIView.animate(withDuration: 1.0, delay: 0.0, options:[.transitionCrossDissolve], animations: {
self.view1.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.view2.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00)
self.view.backgroundColor = UIColor(red: 26.00/255, green: 152.00/255, blue: 143.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()
}, completion:nil)
因此,由于我的drawRect
实现,波形视图背景颜色无法动画。我要做的是将背景颜色设置为"波形视图"以清除,然后在其后面添加另一个UIView
并为该视图的背景颜色进行动画。