ios 使用 Swift 从底部向上推视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31970617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Push Up View Controller from Bottom using Swift
提问by Daniel Bramhall
I would like to push a view controller using Swift and animate it so it appears from the bottom and moves up. I have the following code to push my view controller:
我想使用 Swift 推送一个视图控制器并为其设置动画,使其从底部出现并向上移动。我有以下代码来推送我的视图控制器:
let helloTableViewController = self.storyboard!.instantiateViewControllerWithIdentifier("helloTableViewController") as! HelloTableViewController
self.navigationController!.pushViewController(helloTableViewController, animated: true)
I have found the following from another questionbut cannot seem to get it to work in Swift:
我从另一个问题中找到了以下内容,但似乎无法在 Swift 中使用它:
CATransition *animation = [CATransition animation];
[animation setDuration:2];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromTop];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
SecondView *sObj=[[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:sObj animated:YES];
[[sObj.view layer] addAnimation:animation forKey:@"SwitchToView1"];
回答by Besat
Swift3:
斯威夫特3:
for push :
推:
// push view controller but animate modally
let storyBoard: UIStoryboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "myViewControllerIdentifier") as! MyViewController
let navigationController = self.navigationController
vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: vc, action: nil)
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionMoveIn
transition.subtype = kCATransitionFromTop
navigationController?.view.layer.add(transition, forKey: nil)
navigationController?.pushViewController(vc, animated: false)
and in vc for pop:
在 vc 中流行:
func closeView() {
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromBottom
navigationController?.view.layer.add(transition, forKey: nil)
_ = navigationController?.popViewController(animated: false)
}
回答by Nex Mishra
Here is the code for pushing
这是推送的代码
let transition:CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromBottom
self.hostNavController?.view.layer.add(transition, forKey: kCATransition)
self.hostNavController?.popViewController(animated: true)
and type of transition you can use are
您可以使用的过渡类型是
kCATransitionFromLeft
kCATransitionFromBottom
kCATransitionFromRight
kCATransitionFromTop
回答by Tamás Sengel
Swift 4.2+ solution based on Nex Mishra's answer:
基于Nex Mishra 回答的Swift 4.2+ 解决方案:
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
transition.type = .push
transition.subtype = .fromBottom
navigationController?.view.layer.add(transition, forKey: kCATransition)
navigationController?.pushViewController([destination VC], animated: false)
回答by Starsky
I had a similar situation where I needed to show a navigation item with some UIBarButtonItems on a view controller, but when presenting modally, it wasn't showing up. It was showing only when pushing from a navigationController, but I really needed the animation from bottom -> up. I tried the CATransition examples from above, but it brought up another bug where there was like a black shadow / translucent background during the animation. I came up with a pretty simple solution in the end:
我有一个类似的情况,我需要在视图控制器上显示带有一些 UIBarButtonItems 的导航项,但是当以模态呈现时,它没有显示出来。它仅在从导航控制器推送时显示,但我真的需要从底部 - > 向上的动画。我尝试了上面的 CATransition 示例,但它带来了另一个错误,在动画过程中出现黑色阴影/半透明背景。最后我想出了一个非常简单的解决方案:
let someVC = storyboard?.instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
let navController = UINavigationController(rootViewController: someVC)
navigationController?.present(navController, animated: true, completion: nil)
And to close the view controller and get rid of the navigation controller altogether, you just call:
要关闭视图控制器并完全摆脱导航控制器,您只需调用:
self.dismiss(animated: true, completion: nil)
This will dismiss the view controller, and will release our navigationController from memory.
这将关闭视图控制器,并将从内存中释放我们的 navigationController。
It is a simple solution in the end, but I spent some time until coming up with it. Maybe I'll save someone else's time :)
最后这是一个简单的解决方案,但我花了一些时间才想出它。也许我会节省别人的时间:)
回答by COVID19
Swift 5.0 And Later
Swift 5.0 及更高版本
let transition:CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.push
transition.subtype = CATransitionSubtype.fromTop
self.navigationController!.view.layer.add(transition, forKey: kCATransition)
回答by Jogendra.Com
Updated with Swift 5 and Xcode 11.4
使用 Swift 5 和 Xcode 11.4 更新
Controller push in present animation style
当前动画风格的控制器推送
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let transition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.moveIn
transition.subtype = CATransitionSubtype.fromTop
self.navigationController?.view.layer.add(transition, forKey: nil)
self.navigationController?.pushViewController(viewController, animated: false)
PopToView Controller in Dismiss Style
Dismiss 风格的 PopToView 控制器
let transition:CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeInEaseOut)
transition.type = CATransitionType.reveal
transition.subtype = CATransitionSubtype.fromBottom
self.navigationController?.view.layer.add(transition, forKey: kCATransition)
self.navigationController?.popViewController(animated: false)