1
我想通过不同的颜色使背景颜色循环。iOS Swift - 改变背景颜色
我发现的代码做在Objective-C在这里:
- (void) doBackgroundColorAnimation {
static NSInteger i = 0;
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor whiteColor], [UIColor blackColor], nil];
if(i >= [colors count]) {
i = 0;
}
[UIView animateWithDuration:2.0f animations:^{
self.view.backgroundColor = [colors objectAtIndex:i];
} completion:^(BOOL finished) {
++i;
[self doBackgroundColorAnimation];
}];
}
然而,我的SWIFT代码是不工作?我在完成方法中打印出“完成”一词,但由于某种原因,它会像控制台一直被调用一样对控制台进行垃圾处理?
我在做什么错?
import UIKit
class ViewController: UIViewController {
override func prefersStatusBarHidden() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
self.tripOut()
}
func tripOut() {
var i = 0
let colors = [UIColor.redColor(),UIColor.blueColor(),UIColor.yellowColor()]
if(i >= colors.count) {
i = 0
}
UIView.animateWithDuration(2.0, animations: {() -> Void in
self.view.backgroundColor = colors[i]
}, completion: { (value: Bool) in
++i
self.tripOut()
println("done")
})
}
}
因为'tripOut'调用自身。永远。 – matt
有没有办法让它调用一次? – Josh
您可以使用一个标志并为该标志添加一个条件,并且在任何时候都会停止动画。 –