![](/img/trans.png)
[英]Adding a view in a completion block of UIView.animateWithDuration
[英]UIView.animateWithDuration doesn't animate on separate view controller
将动画设置为反复淡入和淡出。 “ viewSmall”和“ viewBig”是UIImageViews。
class TestViewController: UIViewController {
@IBOutlet weak var viewBig: UIImageView!
@IBOutlet weak var viewSmall: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
animate()
}
func animate() {
UIView.animateWithDuration(0.5,
delay: 0,
options: [.Repeat, .Autoreverse],
animations: {
self.viewSmall.alpha = 0
},
completion: nil)
UIView.animateWithDuration(0.5,
delay: 0,
options: [.Repeat, .Autoreverse],
animations: {
self.viewBig.alpha = 0
},
completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这段代码不会在仅有的几个视图控制器上进行动画处理,而且我认为这些视图控制器似乎是独立的,与其他视图控制器无关。 因为此代码在连接到导航视图控制器的视图控制器上可以完美工作。
如何在独立的视图控制器上制作它? 请帮忙。
找到了一个绝妙的解决方案。 使用dispatch_after捕获持续时间和延迟。
func animate(){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
// Animate the transition
UIView.animateWithDuration(0.5,
delay: 0,
options: [.Repeat, .Autoreverse],
animations: {
self.viewSmall.alpha = 0
},
completion: nil)
UIView.animateWithDuration(0.5,
delay: 0,
options: [.Repeat, .Autoreverse],
animations: {
self.viewBig.alpha = 0
},
completion: nil)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.