gpt4 book ai didi

ios - 在 Swift 3.0 中动画完成后导航到新 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:09:51 26 4
gpt4 key购买 nike

我有 (2) 个目标:

  1. 示例#1:在 Swift 3.0 中完成以下动画后,从原始 View Controller 导航到新的 SecondViewController(即用户不必按任何按钮,应用程序只需在动画结束后移动到 SecondViewController 即可)动画完成)

  2. 示例#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 设置:

enter image description here

最佳答案

使用DispatchQueue.main.asyncAfter

已编辑

将 Storyboard ID 设置为 SecondViewController。

enter image description here

示例#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com