【问题标题】:Repeat/Autoreverse animations in iOS 13.xiOS 13.x 中的重复/自动反转动画
【发布时间】:2020-02-21 05:57:19
【问题描述】:

以前在 swift 中你可以这样做:

let animator = UIViewPropertyAnimator(duration: 0.25, curve: .easeIn) {
  UIView.setAnimationRepeatCount(Float.infinity)
  UIView.setAnimationRepeatAutoreverses(true)
  let transform = CATransform3DIdentity
  let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
  self.ex.layer.transform = rotate
}

但是,现在 UIView.setAnimationRepeatCount 和 UIView.setAnimationRepeatAutoreverses 上有一条弃用消息。有人知道他们被什么替换了吗?我仍然可以使用UIViewPropertyAnimator,还是必须使用CABasicAnimation 之类的东西?

消息是:

'setAnimationRepeatCount' 在 iOS 13.0 中已弃用:改用基于块的动画 API

“setAnimationRepeatAutoreverses”在 iOS 13.0 中已弃用:改用基于块的动画 API

【问题讨论】:

    标签: swift ios13 uiviewpropertyanimator


    【解决方案1】:

    你可以这样做:

    UIView.animate(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
        let transform = CATransform3DIdentity
        let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
        self.ex.layer.transform = rotate
    }, completion: nil)
    

    对于所有可能的呼叫,您可以查看this link

    另外,如果你真的需要UIViewPropertyAnimator,它有一个similar init:

     UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
        let transform = CATransform3DIdentity
        let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
        self.ex.layer.transform = rotate
    })
    

    【讨论】:

    • 是否可以使用UIViewPropertyAnimator 方法并具有相同的功能,或者Apple 是否对其进行了更改,以便该方法只能用于非重复动画?
    • 是的,问题在于UIView 中已弃用的setAnimationRepeatCount 和setAnimationRepeatAutoreverses 函数,Apple 开始将这些选项放入代码块中。刚刚编辑了答案。
    • 对于其他人来说,UIViewPropertyAnimator API 会忽略 .autoreverse 和 .repeat 选项
    • 是的,这对我不起作用。对 UIViewPropertyAnimator 使用 .repeat 不起作用。
    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多