【问题标题】:How to create a time delay with animation in Swift 3.0如何在 Swift 3.0 中使用动画创建时间延迟
【发布时间】:2017-12-03 05:31:28
【问题描述】:
  1. 首先我们生成一个随机数(例如从 0 到 10):

    If randomNumber = 0
        Animate imageSet0 [here we create an ImageArray and an animate function – please see code below)
    Else if randomNumber = 1
        Animate imageSet1
    Else if randomNumber = 2
        Animate imageSet2
    And so on…
    
  2. 然后我们放置一个DispatchQueue定时器,等待上述动画完成(延时等于animationDuration),然后我们重复上面的第一步,生成另一个随机数,播放另一个动画集:

    DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {
       [Insert code that repeats the first step above and generates another random number to play another animation set]
    }
    
  3. 理论上,这个随机动画可以无限期地播放,直到用户移过这个场景。

到目前为止,这是我的代码:

func createImageArray(total: Int, imagePrefix: String) -> [UIImage]{
    var imageArray: [UIImage] = []
    for imageCount in 0..<total {
        let imageName = "\(imagePrefix)-\(imageCount).png"
        let image = UIImage(named: imageName)!

        imageArray.append(image)
    }

    return imageArray

}


func animate(imageView: UIImageView, images: [UIImage]){
    imageView.animationImages = images
    imageView.animationDuration = 1.0
    imageView.animationRepeatCount = 1
    imageView.startAnimating()

    DispatchQueue.main.asyncAfter(deadline: .now() + imageView.animationDuration) {

[Create code that repeats the first step above and generates another random number to play another animation set]

    }
}

【问题讨论】:

    标签: ios swift animation imageview


    【解决方案1】:

    您可以为此使用UIView.animate(withDuration: delay: options: animations:) api。注意延迟参数会让你在一定的延迟后开始动画。

    【讨论】:

    • 嗨@Sandeep - 你将如何使用随机函数使用 UIView.animate api 播放十个动画之一?
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2015-02-20
    • 2015-04-04
    • 1970-01-01
    • 2020-03-26
    • 2023-04-02
    • 2020-11-08
    相关资源
    最近更新 更多