导航到新的视图控制器后,动画在斯威夫特完成3.0
问题描述:
我有(2)目标:导航到新的视图控制器后,动画在斯威夫特完成3.0
示例#1:从原来的视图控制器导航到一个新的SecondViewController以下动画在完成后,迅速3.0(即,用户不必按任何按钮和App简单地移动到SecondViewController动画完成后)
实施例#2:从原始视图控制器导航到新的SecondViewController一个时间延迟之后在Swift 3.0中5秒(即用户不必按任何按钮和应用程序si mply移动到SecondViewController计时器之后到达5秒 - 没有动画涉及第二示例中,只是一个定时器)
这是我的对实施例#1的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
var imagesName = ["Image_1","Image_2","Image_3","Image_4","Image_5","Image_6","Image_7","Image_8","Image_9","Image_10","Image_11","Image_12","Image_13","Image_14","Image_15"]
var images = [UIImage]()
for i in 0..<imagesName.count{
images.append(UIImage(named: imagesName[i])!)
}
imageView.animationImages = images
imageView.animationDuration = 1.0
imageView.startAnimating()
// Perhaps here is where we can fire the code to programatically move to a new View Controller after the animation is completed
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的Xcode中设置:
答
使用DispatchQueue.main.asyncAfter
。
EDITED
集故事板ID来SecondViewController。
示例#1:
...
imageView.animationRepeatCount = 1 // <-- here
imageView.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
self.show(secondViewController, sender: nil)
}
例子#2:
...
imageView.animationRepeatCount = 1 // <-- here
imageView.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
self.show(secondViewController, sender: nil)
}
由于@Kosuke小川 - 一个后续问题,当原始视图控制器移动到SecondViewController我收到一个没有内容的黑屏。你知道什么可能是错的吗?谢谢 –
你的SecondViewController是否有Xib/Storyboard? –
如果是的话,使用'让secondViewController = self.storyboard .instantiateViewController(withIdentifier:“SecondViewController”)?' –