gpt4 book ai didi

iOS 自定义过渡和旋转

转载 作者:IT王子 更新时间:2023-10-29 05:32:40 26 4
gpt4 key购买 nike

我正在使用自定义过渡来显示全屏模态游戏 View 。当用户开始游戏时, Root View Controller 缩小“进入”屏幕,而全屏游戏 View Controller 从更大的比例缩小到显示器上,同时从 0% 不透明度过渡到 100% 不透明度。很简单!

过渡看起来很棒并且运行良好,在关闭游戏 View Controller 时也能正确反转动画。

我遇到的问题是,如果在显示全屏游戏 View Controller 时旋转设备,返回到 Root View Controller 时,布局就会一团糟。进一步旋转并不能解决问题,布局很乱,而且一直很乱。

如果我禁用自定义转换,这个问题就消失了。此外,如果我保留自定义过渡,但在过渡动画中禁用在我的源 View 和目标 View 上设置 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
}
}

并且,为了视觉引用,这是我的 Root View Controller 通常的样子:

Correct layout for my root view controller

并且,为了视觉引用,如果我在游戏 View 中旋转我的设备(至少一次)并返回到 Root View Controller ,这是我的 Root View Controller 的样子。

Broken layout, after rotating device while in fullscreen game view and returning to root view

最后一点——以防万一——我正在使用自动布局和大小类来布置我的 Root View Controller 。

谢谢,

最佳答案

我有一个类似的问题,我在导航 Controller 上为推送和弹出编写了一个自定义转换。如果您在按下后旋转设备,当您返回到 Root View Controller 时,它的框架将保持不变。


问题

enter image description here


解决方案

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 ...
}

关于iOS 自定义过渡和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31969524/

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