【发布时间】:2015-08-12 15:27:23
【问题描述】:
我正在使用自定义转换来显示全屏模式游戏视图。当用户开始游戏时,根视图控制器缩小“进入”屏幕,而全屏游戏视图控制器从较大的比例缩小到显示器,同时从 0% 不透明度过渡到 100% 不透明度。简单!
过渡看起来很棒并且工作正常,在关闭游戏视图控制器时也正确地反转动画。
我遇到的问题是,如果在显示全屏游戏视图控制器的同时旋转设备,在返回到根视图控制器时,布局会变得很糟糕。进一步的旋转并不能解决问题,布局很乱,而且一直很乱。
如果我禁用自定义过渡,这个问题就消失了。此外,如果我保留自定义过渡,但在过渡动画的源视图和目标视图上禁用调用设置 CATransform3D,问题就会再次消失。
这是我的过渡代表:
class FullscreenModalTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
private var presenting:Bool = true
// MARK: UIViewControllerAnimatedTransitioning protocol methods
func animateTransition(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.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
let scale:CGFloat = 1.075
//let bigScale = CGAffineTransformMakeScale(scale, scale)
//let smallScale = CGAffineTransformMakeScale(1/scale,1/scale)
let bigScale = CATransform3DMakeScale(scale, scale, 1)
let smallScale = CATransform3DMakeScale(1/scale, 1/scale, 1)
let smallOpacity:CGFloat = 0.5
let presenting = self.presenting
if presenting {
// when presenting, incoming view must be on top
container.addSubview(fromView)
container.addSubview(toView)
toView.layer.transform = bigScale
toView.opaque = false
toView.alpha = 0
fromView.layer.transform = CATransform3DIdentity
fromView.opaque = false
fromView.alpha = 1
} else {
// when !presenting, outgoing view must be on top
container.addSubview(toView)
container.addSubview(fromView)
toView.layer.transform = smallScale
toView.opaque = false
toView.alpha = smallOpacity
fromView.layer.transform = CATransform3DIdentity
fromView.opaque = false
fromView.alpha = 1
}
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0,
options: nil,
animations: {
if presenting {
fromView.layer.transform = smallScale
fromView.alpha = smallOpacity
} else {
fromView.layer.transform = bigScale
fromView.alpha = 0
}
},
completion: nil )
UIView.animateWithDuration(duration,
delay: duration/6,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: nil,
animations: {
toView.layer.transform = CATransform3DIdentity
toView.alpha = 1
},
completion: { finished in
transitionContext.completeTransition(true)
})
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0.5
}
// MARK: UIViewControllerTransitioningDelegate protocol methods
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = true
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
self.presenting = false
return self
}
}
并且,作为视觉参考,这是我的根视图控制器通常的样子:
并且,作为视觉参考,如果我在游戏视图中旋转我的设备(至少一次)并返回到根视图控制器,这就是我的根视图控制器的样子。
最后一点 - 以防它响起任何铃声 - 我正在使用自动布局和大小类来布局我的根视图控制器。
谢谢,
【问题讨论】:
标签: ios swift uiviewcontroller transitions