UIViewControllerAnimatedTransitioning不会为帧大小设置动画

时间:2017-09-19 06:02:19

标签: ios swift animation uiviewcontroller

我正在尝试使用Pop-in效果在UIViewControllers之间进行转换动画。

在我的animateTransition(using transitionContext:)函数中:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView
    if let toController = transitionContext.viewController(forKey: .to) {
        let toView = toController.view!

        let finalFrame = transitionContext.finalFrame(for: toController) // (20, 20, 200, 200)
        containerView.addSubview(toView)

        toView.frame = CGRect(x: 20, y: 20, width: 100, height: 100)

        UIView.animate(withDuration: self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .allowAnimatedContent, animations: {
            toView.frame = finalFrame
        }, completion: {(completed: Bool) -> Void in
            transitionContext.completeTransition(completed)
        })
    }
}

我原以为框架的大小会从100 by 100变为200 by 200。但它没有。

我发现此功能可以在转换时为框架的位置设置动画,但不能为尺寸(宽度或高度)设置动画。如何设置框架大小的动画?

2 个答案:

答案 0 :(得分:1)

您可以尝试此操作,而不是更改使用新尺寸设置新框架

UIView.animate(withDuration: self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .allowAnimatedContent, animations: {
            toView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        }, completion: {(completed: Bool) -> Void in
            transitionContext.completeTransition(completed)
        })

答案 1 :(得分:1)

这可能是因为您可能在应用中使用了自动布局 布局引擎启动后,根据原始约束重新定位视图 尝试修改约束(或应用转换),而不是框架。使用自动布局修改框架几乎没用。

相关问题