gpt4 book ai didi

swift - 自定义 UINavigationController Segue Swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:58 24 4
gpt4 key购买 nike

这里有一个很棒的 Swift 自定义动画转场教程:

http://mathewsanders.com/animated-transitions-in-swift/

我是初学者,不知道如何使用相同的技术为 UINavigationController 创建自定义转场

VC代码:

class FlashCardView: UIViewController, UINavigationControllerDelegate  {

let transitionManager = TransitionManager()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let toViewController = segue.destinationViewController as UIViewController

toViewController.transitioningDelegate = self.transitionManager
}
/* ... */
}

TransitionManager类代码:

import UIKit

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

private var presenting = true

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
println("test1")

let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!


let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)

if (self.presenting){
toView.transform = offScreenRight
}
else {
toView.transform = offScreenLeft
}

container.addSubview(toView)
container.addSubview(fromView)

let duration = self.transitionDuration(transitionContext)

UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 5, initialSpringVelocity: 0.8, options: nil, animations: {

if (self.presenting){
fromView.transform = offScreenLeft
}
else {
fromView.transform = offScreenRight
}
toView.transform = CGAffineTransformIdentity

}, completion: { finished in
transitionContext.completeTransition(true)
})

}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
println("test2")

return 0.5
}

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
println("test3")
self.presenting = true
return self
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
println("test4")
self.presenting = false
return self
}
}

最佳答案

幸运的是我从那个网站学到了转换,这是一个很棒的博客。似乎您不希望在过渡结束时出现弹跳效果。使用正常的 animatewithduration。

这是满足您需求的有效解决方案:

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate  {


func animateTransition(transitionContext: UIViewControllerContextTransitioning)
{

let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!


let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)



toView.transform = offScreenRight

// add the both views to our view controller
container.addSubview(toView)
container.addSubview(fromView)

let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(0.8, delay: 0.0, options: nil, animations: {

fromView.transform = offScreenLeft
toView.transform = CGAffineTransformIdentity

}, completion: { finished in

transitionContext.completeTransition(true)

})
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
return 0.5
}

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}

关于swift - 自定义 UINavigationController Segue Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944475/

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