【发布时间】:2018-03-21 17:26:29
【问题描述】:
我正在尝试使用 UIViewControllerAnimatedTransitioning 委托方法为 viewController 设置动画,但我有一些问题。
我见过的所有示例代码都使用框架来为视图设置动画,但我使用的是自动布局,它在为框架更改设置动画时给我带来了问题。
我想以这种方式动画从 firstVC 到 secondVC 的过渡:
所以,这是我用来做这个动画的代码:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let fromVC = transitionContext.viewController(forKey: .from)! as! CustomView
let toView = transitionContext.view(forKey: .to)!
//let cell = fromVC.currentCell //Access to the pressed cell (if needed)
toView.frame = fromVC.currentFrame //currentFrame is the cell frame saved on fromVC
toView.autoresizingMask = containerView.autoresizingMask
toView.layoutIfNeeded()
containerView.addSubview(toView)
UIView.animate(withDuration: duration, animations: {
toView.frame = containerView.frame
toView.layoutIfNeeded()
}, completion: { _ in
transitionContext.completeTransition(true)
})
}
如您所见,由于帧修改和使用 AutoLayout,动画效果不佳。
那么,你认为我可以如何在有约束的情况下制作这个动画?
谢谢!
【问题讨论】:
-
如果你可以分享,我可以检查你的布局。
-
我无法编辑我的问题,但我发现了一些奇怪的东西。如果我在标签上放置一个背景颜色,我可以看到如何遵守约束,但文本从右到左显示,而不是 textAlignment = .center
标签: ios swift xcode autolayout constraints