Ios UIViewControlleranimated转换赢得';设置帧大小的动画
我正试图用Pop效果在Ios UIViewControlleranimated转换赢得';设置帧大小的动画,ios,swift,animation,uiviewcontroller,Ios,Swift,Animation,Uiviewcontroller,我正试图用Pop效果在UIViewControllers之间设置转换动画 在myanimateTransition(使用transitionContext:)函数中: func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerView = transitionContext.containerView if let toControlle
UIViewControllers
之间设置转换动画
在myanimateTransition(使用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乘100
更改为200乘200
。但事实并非如此
我发现这个函数可以在转换时设置帧的位置的动画,但不能设置大小的动画(宽度或高度)。如何设置帧大小的动画?您可以尝试此操作,而不是使用新大小更改设置新帧
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)
})
这是因为您可能正在应用程序中使用自动布局。
布局引擎启动后,将根据原始约束重新定位视图。
尝试修改约束(或应用变换),而不是帧。使用自动布局修改框架几乎毫无用处。还没有尝试过,但我想你是对的,自动布局导致了这个问题。谢谢你的建议,但我需要在实际项目中保持比例因子。