Swift - UIViewControllerAnimatedTransitioning 未按预期转换 Swift4
Swift - UIViewControllerAnimatedTransitioning not transform as expected with Swift4
我今天刚用 Swift4 升级到 XCode9。
而且我发现我的 UIViewControllerAnimatedTransitioning
不再按预期工作。
这个动画的效果是fromView
会缩小到0.95,toView
会从右侧滑入。 pop
操作将相反。
但是现在,当我点击 NavigationBar 的后退按钮时,toView
的起始位置不正确。它显示原始尺寸 toView
,然后放大到 1.05。
下面是我实现过渡动画的方法。
// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// get reference to our fromView, toView and the container view that we should perform the transition in
let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
// set up from 2D transforms that we'll use in the animation
let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
let offScreenDepth = CGAffineTransform(scaleX: 0.95, y: 0.95)
// start the toView to the right of the screen
if( presenting ){
toView.transform = offScreenRight
container.addSubview(fromView)
container.addSubview(toView)
}
else{
toView.transform = offScreenDepth
container.addSubview(toView)
container.addSubview(fromView)
}
// get the duration of the animation
// DON'T just type '0.5s' -- the reason why won't make sense until the next post
// but for now it's important to just follow this approach
let duration = self.transitionDuration(using: transitionContext)
// perform the animation!
// for this example, just slid both fromView and toView to the left at the same time
// meaning fromView is pushed off the screen and toView slides into view
// we also use the block animation usingSpringWithDamping for a little bounce
UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {
if( self.presenting ){
fromView.transform = offScreenDepth
}
else{
fromView.transform = offScreenRight
}
toView.transform = .identity
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
我在此迁移指南页面中没有发现任何特别之处。
https://swift.org/migration-guide-swift4/
我应该怎么做才能使转换再次生效?
在设置任何其他内容之前,请尝试将 fromView
的转换重置为 identity
:
// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
...
UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {
if( self.presenting ){
fromView.transform = offScreenDepth
}
else{
fromView.transform = offScreenRight
}
toView.transform = .identity
}, completion: { finished in
fromView.transform = .identity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
....
}
我今天刚用 Swift4 升级到 XCode9。
而且我发现我的 UIViewControllerAnimatedTransitioning
不再按预期工作。
这个动画的效果是fromView
会缩小到0.95,toView
会从右侧滑入。 pop
操作将相反。
但是现在,当我点击 NavigationBar 的后退按钮时,toView
的起始位置不正确。它显示原始尺寸 toView
,然后放大到 1.05。
下面是我实现过渡动画的方法。
// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// get reference to our fromView, toView and the container view that we should perform the transition in
let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
// set up from 2D transforms that we'll use in the animation
let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
let offScreenDepth = CGAffineTransform(scaleX: 0.95, y: 0.95)
// start the toView to the right of the screen
if( presenting ){
toView.transform = offScreenRight
container.addSubview(fromView)
container.addSubview(toView)
}
else{
toView.transform = offScreenDepth
container.addSubview(toView)
container.addSubview(fromView)
}
// get the duration of the animation
// DON'T just type '0.5s' -- the reason why won't make sense until the next post
// but for now it's important to just follow this approach
let duration = self.transitionDuration(using: transitionContext)
// perform the animation!
// for this example, just slid both fromView and toView to the left at the same time
// meaning fromView is pushed off the screen and toView slides into view
// we also use the block animation usingSpringWithDamping for a little bounce
UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {
if( self.presenting ){
fromView.transform = offScreenDepth
}
else{
fromView.transform = offScreenRight
}
toView.transform = .identity
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
我在此迁移指南页面中没有发现任何特别之处。 https://swift.org/migration-guide-swift4/ 我应该怎么做才能使转换再次生效?
在设置任何其他内容之前,请尝试将 fromView
的转换重置为 identity
:
// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
...
UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {
if( self.presenting ){
fromView.transform = offScreenDepth
}
else{
fromView.transform = offScreenRight
}
toView.transform = .identity
}, completion: { finished in
fromView.transform = .identity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
....
}