iOS 自定义过渡和旋转
iOS custom transitions and rotation
我正在使用自定义转换来显示全屏模态游戏视图。当用户开始游戏时,根视图控制器会缩小 "into" 屏幕,而全屏游戏视图控制器会从较大的比例缩小到显示屏上,同时从 0% 不透明度过渡到 100% 不透明度。简单!
过渡看起来很棒并且运行良好,在关闭游戏视图控制器时也能正确反转动画。
我遇到的问题是,如果在显示全屏游戏视图控制器时旋转设备,在return转到根视图控制器时,布局很奇怪。进一步旋转并不能解决问题,布局很乱,而且一直很乱。
如果我禁用自定义转换,这个问题就消失了。此外,如果我保留自定义过渡,但 禁用调用在过渡动画中的源视图和目标视图上设置 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
}
}
并且,为了视觉参考,这是我的根视图控制器通常的样子:
而且,为了视觉参考,如果我在游戏视图中旋转我的设备(至少一次),并且 return 到根视图控制器,我的根视图控制器看起来像这样。
最后一点 - 以防万一 - 我正在使用自动布局和大小 类 来布置我的根视图控制器。
谢谢,
我在对具有非恒等式转换的视图执行一些手动布局代码时无意中发现了这一点。 事实证明,如果您的视图具有非恒等变换,正常布局代码将失败。
在我的转换委托中,解决方法是采用转换出的视图,并在动画完成回调中将其转换设置为身份(因为该视图在动画结束时不可见,在新视图后面,这对外观没有影响)
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0,
options: nil,
animations: {
if presenting {
fromView.transform = smallScale
fromView.alpha = smallOpacity
} else {
fromView.transform = bigScale
fromView.alpha = 0
}
},
completion: { completed in
// set transform of now hidden view to identity to prevent breakage during rotation
fromView.transform = CGAffineTransformIdentity
})
我遇到了类似的问题,我在导航控制器上为推送和弹出编写了自定义转换。如果您在按下后旋转设备,当您返回到根视图控制器时,它的框架将保持不变。
问题
解决方案
Objective C
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
// ViewController Reference
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Fix layout bug in iOS 9+
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
// The rest of your code ...
}
Swift 3.0
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// ViewController reference
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
// Fix layout bug in iOS 9+
toViewController.view.frame = transitionContext.finalFrame(for: toViewController)
// The rest of your code ...
}
我终于找到了这个问题的解决方案。您需要改进@agilityvision 代码。您需要添加 BOOL 值,例如 closeVCNow 表示您要关闭 VC,在 animateTransition:
中,在动画块中执行此操作:
if (self.closeVCNow) {
toVC.view.transform = CGAffineTransformIdentity;
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
self.closeVCNow = NO;
}
我正在使用自定义转换来显示全屏模态游戏视图。当用户开始游戏时,根视图控制器会缩小 "into" 屏幕,而全屏游戏视图控制器会从较大的比例缩小到显示屏上,同时从 0% 不透明度过渡到 100% 不透明度。简单!
过渡看起来很棒并且运行良好,在关闭游戏视图控制器时也能正确反转动画。
我遇到的问题是,如果在显示全屏游戏视图控制器时旋转设备,在return转到根视图控制器时,布局很奇怪。进一步旋转并不能解决问题,布局很乱,而且一直很乱。
如果我禁用自定义转换,这个问题就消失了。此外,如果我保留自定义过渡,但 禁用调用在过渡动画中的源视图和目标视图上设置 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
}
}
并且,为了视觉参考,这是我的根视图控制器通常的样子:
而且,为了视觉参考,如果我在游戏视图中旋转我的设备(至少一次),并且 return 到根视图控制器,我的根视图控制器看起来像这样。
最后一点 - 以防万一 - 我正在使用自动布局和大小 类 来布置我的根视图控制器。
谢谢,
我在对具有非恒等式转换的视图执行一些手动布局代码时无意中发现了这一点。 事实证明,如果您的视图具有非恒等变换,正常布局代码将失败。
在我的转换委托中,解决方法是采用转换出的视图,并在动画完成回调中将其转换设置为身份(因为该视图在动画结束时不可见,在新视图后面,这对外观没有影响)
UIView.animateWithDuration(duration,
delay: 0.0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0,
options: nil,
animations: {
if presenting {
fromView.transform = smallScale
fromView.alpha = smallOpacity
} else {
fromView.transform = bigScale
fromView.alpha = 0
}
},
completion: { completed in
// set transform of now hidden view to identity to prevent breakage during rotation
fromView.transform = CGAffineTransformIdentity
})
我遇到了类似的问题,我在导航控制器上为推送和弹出编写了自定义转换。如果您在按下后旋转设备,当您返回到根视图控制器时,它的框架将保持不变。
问题
解决方案
Objective C
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
// ViewController Reference
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Fix layout bug in iOS 9+
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
// The rest of your code ...
}
Swift 3.0
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// ViewController reference
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
// Fix layout bug in iOS 9+
toViewController.view.frame = transitionContext.finalFrame(for: toViewController)
// The rest of your code ...
}
我终于找到了这个问题的解决方案。您需要改进@agilityvision 代码。您需要添加 BOOL 值,例如 closeVCNow 表示您要关闭 VC,在 animateTransition:
中,在动画块中执行此操作:
if (self.closeVCNow) {
toVC.view.transform = CGAffineTransformIdentity;
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
self.closeVCNow = NO;
}