我有 (2) 个目标:
示例#1:在 Swift 3.0 中完成以下动画后,从原始 View Controller 导航到新的 SecondViewController(即用户不必按任何按钮,应用程序只需在动画结束后移动到 SecondViewController 即可)动画完成)
示例#2:在 Swift 3.0 中,经过 5 秒的时间延迟后,从原始 View Controller 导航到新的 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
。
已编辑
将 Storyboard 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)
}
关于ios - 在 Swift 3.0 中动画完成后导航到新 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46209316/