【问题标题】:How to synchronise UIViewControllerInteractiveTransitioning and CABasicAnimation如何同步 UIViewControllerInteractiveTransitioning 和 CABasicAnimation
【发布时间】:2020-10-01 15:00:11
【问题描述】:

我在视图控制器中有一个带有阴影的按钮。在 viewWillAppear 中,此阴影与动画一起应用。

按钮位于名为“buttonContainer”的空视图中。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    CATransaction.begin()
    CATransaction.setCompletionBlock {
        self.buttonContainer.layer.shadowOpacity = 1
    }
    
    let animation = CABasicAnimation(keyPath: "shadowOpacity")
    animation.fromValue = buttonContainer.layer.shadowOpacity
    animation.toValue = 1
    animation.duration = 0.6
    buttonContainer.layer.add(animation, forKey: animation.keyPath)
    
    CATransaction.commit()
}

我希望阴影通过视图控制器的推送动画进行动画处理。我开始实现“UIViewControllerAnimatedTransitioning”,但找不到将动画持续时间与 CABasicAnimation 联系起来的方法。

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    let containerView = transitionContext.containerView
    let button = containerView.viewWithTag(68) as! UIButton

    let animation = CABasicAnimation(keyPath: "shadowOpacity")
    animation.fromValue = ?
    animation.toValue = ?
}

我想我需要实现 UIViewControllerInteractiveTransitioning,但我想不出一种方法来配合 CABasicAnimation。任何帮助表示赞赏

【问题讨论】:

  • 好问题,我也有同样的问题(目前使用硬编码的持续时间)!
  • 我正在为bounds 属性设置动画,到目前为止,我唯一想到的是使用CADisplayLink 将其与UIView 同步。签出this

标签: ios swift animation


【解决方案1】:

在做了一些阅读之后,似乎有人在较低级别的 CALayer 动画和“UIViewControllerInteractiveTransitioning”之间建立了一座桥梁

https://github.com/stringcode86/UIPercentDrivenInteractiveTransitionWithCABasicAnimation/blob/master/InteractiveTransition/SCPercentDrivenInteractiveTransition.m

但它是很久以前完成的,我还没有时间验证它。可以肯定的一件事是它看起来很丑。

UIViewPropertyAnimator 类是在 iOS 10 中发布的,旨在通过提供与“addAnimation”和“addCompletion”等函数的协调来帮助解决这种桥接问题。

这些人已经设法通过UIViewPropertyAnimator 实现了一个干净的解决方案,将“CALayer”的动画与“UIViewControllerInteractiveTransitioning”连接起来。

https://github.com/hedjirog/CustomPresentation

总结 太难了。不要打扰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多