对于`UIView.animate`,是否可以*链*Swift Trailing Closures?

问题描述 投票:0回答:1

在Swift中,我希望有几个调用到 UIView.animate 串联运行。 也就是说,当一个动画完成后,我希望另一个动画在之后继续,以此类推。

调用 UIView.animate 拥有 尾部封闭 目前我正用它来进行第二次调用。UIView.animate 要发生。

问题是:"我想做N个独立的动画。 我想做N个独立的动画

来自苹果公司的文档 UIView.animate

completion 动画序列结束时要执行的块对象。这个块没有返回值,并且取一个布尔参数,表示动画是否在完成处理程序被调用之前实际完成。如果动画的持续时间为0,则在下一个运行循环周期开始时执行这个块。这个参数可以是NULL。


理想的情况是,我希望在动画的数组上迭代一个动画 duration并用于调用 animate()

例如:

目标

迭代一个数组,并将这些参数应用于每个动画。

  • let duration = [3.0, 5.0, 10.0]
  • let alpha = [0.1, 0.5, 0.66]
  • Xcode版本11.4.1 (11E503a)

我试过的

使用 map 来迭代,而🤞希望它能成功。

  • 问题是,只有最后的动画发生。 因此,在系列中没有任何东西
  • 研究问题:我是否需要在 "研究 "中设置布尔值?UIView 说动画是连续出现的?
let redBox = UIView()
redBox.backgroundColor = UIColor.red
self.view.addSubview(redBox)

let iterateToAnimate = duration.enumerated().map { (index, element) -> Double in
  print(index, element, duration[index])

  UIView.animate(withDuration: duration[index],  // set duration from the array
                 animations:  { () in
                   redBox.alpha = alpha[index]
                 }, completion:{(Bool)  in
                      print("red box has faded out")
  })
}

如何做一个动画

let redBox = UIView()
redBox.backgroundColor = UIColor.red
self.view.addSubview(redBox)

// One iteration of Animation
UIView.animate(withDuration: 1,
               animations:  { () in
                 redBox.alpha = 0
               }, completion:{(Bool)  in
                    print("red box has faded out")
})

丑陋的方式做链二 animate 迭代

// two iterations of Animation, using a trailing closure
UIView.animate(withDuration: 1,
               animations:  { () in
                 redBox.alpha = 0
               }, completion:{(Bool)  in
                    print("red box has faded out")

}) { _ in  // after first animation finishes, call another in a trailing closure
  UIView.animate(withDuration: 1,
                 animations:  { () in
                   redBox.alpha = 0.75
                 }, completion:{(Bool)  in
                      print("red box has faded out")

  }) // 2nd animation
}
ios swift animation closures uiviewanimation
1个回答
2
投票

如果参数不多,而且在编译时已知,最简单的方法是以关键帧动画的帧数来构造链式动画。

如果参数在编译时不知道(比如你的持续时间要在运行时到达),或者参数很多,写出关键帧动画太麻烦,那么只需要把单个动画和它的完成处理程序放到一个函数中,然后递归。简单的例子(根据自己的目的进行调整)。

var duration = [3.0, 5.0, 10.0]
var alpha    = [0.1, 0.5, 0.66] as [CGFloat]

func doTheAnimation() {
    if duration.count > 0 {
        let dur = duration.removeFirst()
        let alp = alpha.removeFirst()
        UIView.animate(withDuration: dur, animations: {
            self.yellowView.alpha = alp
        }, completion: {_ in self.doTheAnimation()})
    }
}

0
投票

你可以使用 UIView.animateKeyframes

       UIView.animateKeyframes(withDuration: 18.0, delay: 0.0, options: [], animations: {
            UIView.addKeyframe(withRelativeStartTime: 3.0/15.0, relativeDuration: 3.0/15.0, animations: {
                self.redBox.alpha = 0
            })
            UIView.addKeyframe(withRelativeStartTime: 8.0/15.00, relativeDuration: 5.0/15.0, animations: {
               self.redBox.alpha = 0.75
            })
        }) { (completed) in
           print("completed")
        }
© www.soinside.com 2019 - 2024. All rights reserved.