此代码始终运行,并且不等待UI中完成的动画。它应该以三种颜色依次显示淡入和淡出视图。我将淡入淡出功能放入了UIView的扩展中。
CV_LOAD_IMAGE_ANYDEPTH
这是我的淡出扩展名:
import UIKit
class ViewController: UIViewController {
let colorArray = [UIColor.red, UIColor.yellow, UIColor.green]
@IBOutlet weak var fadedView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
fadedView.backgroundColor = UIColor.brown
for color in 0..<colorArray.count {
fadedView.backgroundColor = colorArray[color]
fadedView.bxFadeIn()
fadedView.bxFadeOut()
//UI does not reflect code and shows green background color fade in/out
print("Fades w/ \(color) done")
}
}
}
输出显示循环在动画之前完成。
func bxFadeIn(){
if self.alpha == 0{
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0,
delay: 0,
options: .curveLinear,
animations: {self.alpha = 1},
completion: {finished in print("animation finished \(finished)")}
)
}
}
func bxFadeOut(){
if self.alpha == 1 {
UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0,
delay: 0,
options: .curveLinear,
animations: {self.alpha = 0},
completion: {finished in print("animation finshed \(finished)")})
}
}
}// end of extension
我的完成处理程序有问题吗?