gpt4 book ai didi

ios - Swift - UIViewControllerAnimatedTransitioning 未按预期使用 Swift4 进行转换

转载 作者:行者123 更新时间:2023-11-29 11:41:25 26 4
gpt4 key购买 nike

我今天刚用 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)

})

....
}

关于ios - Swift - UIViewControllerAnimatedTransitioning 未按预期使用 Swift4 进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com