gpt4 book ai didi

ios - 自定义 ViewController 过渡

转载 作者:行者123 更新时间:2023-11-28 06:55:30 25 4
gpt4 key购买 nike

我在 Appcoda Transition 上看到了一个教程ViewControllers transition 一个从上到下的菜单,我实现了。然后,我尝试使用 UIViewControllerContextTransitioning 从下往上进行 transition。但是,做错了,因为我认为我设置了错误的值(value)观。下面是代码

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

//Get reference to our fromView, toView and the container view
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

//Setup the transform for sliding
let container = transitionContext.containerView()
let height = container?.frame.height
let moveDown = CGAffineTransformMakeTranslation(0, height! - 150)
let moveUp = CGAffineTransformMakeTranslation(0, -50)

//Add both views to the container view
if isPresenting {
toView?.transform = moveUp
snapShot = fromView?.snapshotViewAfterScreenUpdates(true)
container?.addSubview(toView!)
container?.addSubview(snapShot!)
}

//Perform the animation
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.3, options: UIViewAnimationOptions(rawValue: 0), animations: {
if self.isPresenting {
self.snapShot?.transform = moveDown
toView?.transform = CGAffineTransformIdentity
} else {
self.snapShot?.transform = CGAffineTransformIdentity
fromView?.transform = moveUp
}
}, completion: {finished in

transitionContext.completeTransition(true)
if !self.isPresenting {
self.snapShot?.removeFromSuperview()
}
})
}

最佳答案

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

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
// Get reference to our fromView, toView and the container view
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

// Set up the transform we'll use in the animation
guard let container = transitionContext.containerView() else {
return
}

let moveUp = CGAffineTransformMakeTranslation(0, container.frame.height + 50)
let moveDown = CGAffineTransformMakeTranslation(0, -250)


// Add both views to the container view
if isPresenting {
toView.transform = moveUp
snapshot = fromView.snapshotViewAfterScreenUpdates(true)
container.addSubview(toView)
container.addSubview(snapshot!)
}

// Perform the animation
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {

if self.isPresenting {
self.snapshot?.transform = moveDown
toView.transform = CGAffineTransformIdentity
} else {
self.snapshot?.transform = CGAffineTransformIdentity
fromView.transform = moveUp
}

}, completion: { finished in

transitionContext.completeTransition(true)

if !self.isPresenting {
self.snapshot?.removeFromSuperview()
}
})
}

这应该有效。我查看了你分享的教程,你可能看不到底部的菜单,因为 MenuTableViewController.swift 在 Storyboard上的设置方式使得菜单总是从顶部开始,所以改变它并它应该工作得很好。如果您有任何问题,请告诉我。

关于ios - 自定义 ViewController 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772694/

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