如何使用Swift语言进行swift 页面跳转动画的跳转

Pages: 1/2
主题 : A页面跳转到B页面 如何关闭A页面
级别: 新手上路
可可豆: 13 CB
威望: 13 点
在线时间: 8(时)
发自: Web Page
来源于&&分类
A页面跳转到B页面 如何关闭A页面&&&
如标题:A页面跳转到B页面 如何关闭A页面我用过1、dismissModalViewControllerAnimated直接关闭2、removeFromSuperview移除view3、NSNotificationCenter传值关闭都不可行,请问那位大侠能指点迷经????我使用的MainStoryboard进行布局最好能带代码,有例子最好,感谢各位高手!!!!~~
级别: 新手上路
可可豆: 13 CB
威望: 13 点
在线时间: 8(时)
发自: Web Page
我的邮箱&&求助!!!!!!
级别: 新手上路
UID: 134656
可可豆: 110 CB
威望: 53 点
在线时间: 57(时)
发自: Web Page
回 1楼(baiooy) 的帖子
为什么要关闭啊???你在把页面置空不就可以了啊??
级别: 新手上路
可可豆: 13 CB
威望: 13 点
在线时间: 8(时)
发自: Web Page
A是登录页面&& B是登录后的页面&& 登录后的页面有关闭按键&&&&点击关闭后有到登录页面了&& 可已经登录不应该再到登录页面 所以我想把登录页面关闭掉
级别: 新手上路
可可豆: 13 CB
威望: 13 点
在线时间: 8(时)
发自: Web Page
如果不关闭我也能控制住跳转但我想这样不好&&&&打算android和iphone的逻辑一样,android有finish,iphone是不是也有我想。。。找了半天都没有找到,&&&& 求助有没有关闭页面的方法!!!~~~~
级别: 新手上路
可可豆: 48 CB
威望: 48 点
在线时间: 20(时)
发自: Web Page
回 4楼(baiooy) 的帖子
楼主问题解决美?&&我也遇到同样的方法&&求答案。。。。谢谢
级别: 新手上路
可可豆: 68 CB
威望: 68 点
在线时间: 330(时)
发自: Web Page
[self.navgationcontroller popViewControllerAnimated:animation];[self.navgationcontroller pushViewController:afterLoginViewController animated:YES];
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 11(时)
发自: Web Page
dispatch_queue_t mainQueue = dispatch_get_current_queue();&&&&dispatch_async(mainQueue, ^(void){&&&&&&&&[self dismissModalViewControllerAnimated:YES];&&&&});楼主,我也遇到了这个问题,用这个解决了
级别: 新手上路
可可豆: 2 CB
威望: 2 点
在线时间: 11(时)
发自: Web Page
前提是上一个&&[self dismissModalViewControllerAnimated:NO]必须 是NO
级别: 新手上路
UID: 307357
可可豆: 87 CB
威望: 86 点
在线时间: 385(时)
发自: Web Page
回 7楼(ncztc) 的帖子
你好,这是怎么解决的, 我也遇到了注册页面的这个问题,求详细指点
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版Swift:超炫的View Controller切换动画 - 推酷
Swift:超炫的View Controller切换动画
匿名社交应用Secret的开发者开发了一款叫做Ping的应用,用户可以他们感兴趣的话题的推送。
Ping有一个很炫的东西,就是主界面和之间切换的动画做的非常的好。每次看到一个非常炫的动画,都不由得会想:“这个东西我要不要自己实现以下”。哈哈~~~
这个教程里,你会学到如何用Swift实现这样的很酷的动画。你会学到如何使用shape layer,遮罩和使用UIViewControllerAnimnatedTransitioning协议和UIPercentDrivenInteractivetransition类等实现View Controller界面切换动画。
不过需要注意,这里假定你已经有一定的Swift开发基础。如果只是初学的话,请自行查看我得其他Swift教程。
我们主要介绍Ping里从一个View Controller跳转到另一个的时候的动画。
在iOS里,你可以在UINavigationController中放入两个View Controller,并实现UIViewControllerAnimatedTransitioning协议来实现界面切换的动画。具体的细节有:
动画的时间长度
创建一个容器View来控制两个View Controller的View
可以实现任意你能想到的动画
这些动画,你可以用UIView得动画方法来作,也可以用core animation这样的比较底层的方法来做。本教程会使用后者。
现在你已经知道代码大概会添加到什么地方。下面讨论下如何实现那个Ping的那个圈圈动画。这动画严格的描述起来是:
圆圈是从右侧的按钮产生。并且从圈中可以看到下面一层试图的内容。
也就是说,这个圆圈是一个遮罩。圆圈里的都可以看到,外面的全部都隐藏。
你可以用CALayer的mask可以达到这个效果。当然还需要设置alpha为0来隐藏下面一个视图的内容。alpha值设定为1的时候显示下面视图的内容。
现在你就懂了遮罩了。下一步就是决定用哪一种CAShapeLayer来实现这个遮罩。只需要修改这些CAShapeLayer组成的圆圈的半径。
这里就不十分详细的叙述了,都是些关于创建和配置项目的步骤。
1. 创建一个新的项目。选择一个single view application
2. 项目名称设置为CircleTransition。语言选择Swift。Devices就选择iPhone
项目到此初步创建好了。在Main.stroyboard里只有一个view controller。但是我们的动画需要两个至少的view controller。不过首先需要把现在的这个view controller和UINavigationController关联起来。选中这个唯一的view controller,之后在菜单栏中选择Editor-&Embed In-&Navigation Controller。之后这个navigation controller就会成为initial controller,后面连着最开始生成的那个view controller。之后,选中这个navigation controller,在右侧菜单栏的第四个tab中勾去“
Shows navigation bar
”。因为在我们的app中不需要navigation bar。
接下来添加另外一个view controller。给这个view controller指定class为ViewController。
然后,给每一个view controller,除了navigation controller,添加一个按钮。双击按钮,删除文字,之后把按钮的背景色设置为黑色。另外一个按钮也同样处理。给这两个按钮设定autolayout。指定他们在右上角上。指定这两个按钮的宽度和高度为40。
最后让按钮变成圆形的。右边菜单的第三个tab中选择“user defined runtime attributes”。点下面的加号,添加如图所示的内容。设置button的corner radius为15。
这样这个按钮在运行起来的时候就是圆形的了。设定完成之后暂时看不到这个效果。运行起来以后:
现在需要在每个view controller中添加些内容了。先把这两个view controller的背景色修改一下。
现在这个app大致已经成型了。不同的颜色可以代表你将来要显示出来的各种各样的内容。所需要的就是把这个两个view controller连起来。在橘色的controller的按钮中放下鼠标。按下ctrl然后把光标拖动到另外一个controller上。这是会出现一个弹出的菜单。把这个菜单的action用同样的方法和这个controller再连接一次,并选择show。这样,在这个按钮选择的时候,navigation controller就会push到下一个view controller中。这是一个segue。后面的教程会需要这个segue所以这里给这个segue一个identifer,叫做“
”。运行代码,点击橘色controller的按钮就会跳转到紫色的controller了。
因为这是一个循环的过程,所以从橘色到紫色之后还需要从紫色回到橘色。现在就完成这个功能。首先,在紫色controller绑定的ViewController类中添加一个action方法。
@IBAction func circleTapped(sender: UIButton){
self.navigationController?.popViewControllerAnimated(true)
并添加紫色controller上的按钮的引用,这个会在后面用到:
@IBOutlet weak var button: UIButton!
之后给紫色controller的按钮的“touch up inside”事件添加上面的@IBAction。
绑定按钮的属性:
再运行起来看看。橘色到紫色,紫色到橘色循环往复!
注意:两个view controller都需要绑定按钮和按钮事件!否则后面的动画只能执行一次!
自定义动画
这里主要处理的就是navigation controller的push和pop动画。这需要我们实现
UINavigationControllerDelegate
animationControllerForOperation
方法。直接在ViewController中添加一个新的类:
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate{
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -& UIViewControllerAnimatedTransitioning? {
return nil
首先,在右侧的菜单中选中Object这个item。
之后,把这个东西拖动到navigation controller secene下。
然后选中这个Object,在右侧菜单的第三个tab上修改class为我们刚刚定义的
NavigationControllerDelegate
下一步,给navigation controller指定delegate。选中navigation controller,然后在右侧最后的菜单中连接navigation controller的delegate选项到刚刚拖进来的Object上:
这个时候还是不会有特定的效果出现。因为方法还是空的,只能算是一个placeholder方法。
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -& UIViewControllerAnimatedTransitioning? {
return nil
这个方法接受两个在navigation controller中得controller。从一个跳转到另一个的两个controller。并返回一个实现了
UIViewControllerAnimatedTransitioning
的对象。所以,我们需要创建一个实现了
UIViewControllerAnimatedTransitioning
协议的类。
class CircleTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning
首先添加一个属性:
weak var transitionContext: UIViewControllerContextTransitioning?
这个属性会在后面的代码中用到。
添加一个方法:
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -& NSTimeInterval {
return 0.5
这个方法返回动画执行的时间。
添加动画方法:
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
self.transitionContext = transitionContext
var containerView = transitionContext.containerView()
var fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as ViewController
var toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as ViewController
var button = fromViewController.button
containerView.addSubview(toViewController.view)
var circleMaskPathInitial = UIBezierPath(ovalInRect: button.frame)
var extremePoint = CGPointMake(button.center.x, button.center.y - CGRectGetHeight(toViewController.view.bounds)) // need more research
var radius = sqrt(extremePoint.x * extremePoint.x + extremePoint.y * extremePoint.y)
var circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(button.frame, -radius, -radius))
var maskLayer = CAShapeLayer()
maskLayer.path = circleMaskPathFinal.CGPath
toViewController.view.layer.mask = maskLayer
var maskLayerAnimation = CABasicAnimation(keyPath: &path&)
maskLayerAnimation.fromValue = circleMaskPathInitial.CGPath
maskLayerAnimation.toValue = circleMaskPathFinal.CGPath
maskLayerAnimation.duration = self.transitionDuration(self.transitionContext!)
maskLayerAnimation.delegate = self
maskLayer.addAnimation(maskLayerAnimation, forKey: &CircleAnimation&)
一步步的解释:
transitionContext属性保持了一个类成员的引用。这样在后面的代码中可以用到。
取出containerView以及fromViewController和toViewController和controller上面的button引用。动画主要还是作用在container view上的。
把toViewController的view添加到container view上。
创建两个路劲,一个就是button的大小(button在运行起来之后是圆形的),另一个要足够大到可以cover整个screen。动画就是在这两个path上来来回回。
CAShapeLayer
作为mask用。给这个layer的path赋值为
circleMaskPathFinal
,否则动画执行完成以后可能又缩回来。
CABasicAnimation
动画,key path是“path”,这个动画作用于layer的path属性上。动画从
circleMaskPathInitial
circleMaskPathFinal
。并给这个动画添加一个delegate,在动画执行完成以后清理现场。
实现animation代理的方法:
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
self.transitionContext?.completeTransition(!self.transitionContext!.transitionWasCancelled())
self.transitionContext?.viewControllerForKey(UITransitionContextFromViewControllerKey)?.view.layer.mask = nil
现在就可以用
CircleTransitionAnimator
来实现动画的效果了。修改代码
NavigationControllerDelegate
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate{
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -& UIViewControllerAnimatedTransitioning? {
return CircleTransitionAnimator()
运行起来吧。点击黑色的按钮,动画效果就出现了。
感觉不错吧,但是这个是不够的!
给动画添加手势响应
我们还要给这个动画添加一个可以响应手势的transition。响应手势需要用到一个方法:navigationController-&interactionControllerForAnimationController。这是UINavigationControllerDelegate中得一个方法。这个方法返回一个实现了协议UIViewControllerInteractiveTransitioning的对象。
iOS的SDK中提供了一个UIPercentDrivenInteractiveTransition的类。这个类实现了上面的协议,并且提供了很多其他的手势处理实现。
在 NavigationControllerDelegate类中添加以下的属性和方法:
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate{
var interactionController: UIPercentDrivenInteractiveTransition?
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -& UIViewControllerAnimatedTransitioning? {
return CircleTransitionAnimator()
func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -& UIViewControllerInteractiveTransitioning? {
return self.interactionController
既然是响应手势的,那么一个pan的手势是必不可少的了。不过首先要添加一些辅助的东西。
NavigationControllerDelegate
中添加对navigation controller的引用。
@IBOutlet weak var navigationController: UINavigationController?
给这个引用添加对navigation controller的引用,如图:
awakeFromNib
override func awakeFromNib() {
super.awakeFromNib()
var pan = UIPanGestureRecognizer(target: self, action: &panned:&)
self.navigationController!.view.addGestureRecognizer(pan)
当pan这个动作在navigation controller的view上发生的时候就会触发panned回调方法。给这个方法添加如下代码:
func panned(gestureRecognizer: UIPanGestureRecognizer){
switch gestureRecognizer.state {
case .Began:
self.interactionController = UIPercentDrivenInteractiveTransition()
if self.navigationController?.viewControllers.count & 1 {
self.navigationController?.popViewControllerAnimated(true)
self.navigationController?.topViewController.performSegueWithIdentifier(&PushSegue&, sender: nil)
case .Changed:
var translation = gestureRecognizer.translationInView(self.navigationController!.view)
var completionProgress = translation.x / CGRectGetWidth(self.navigationController!.view.bounds)
self.interactionController?.updateInteractiveTransition(completionProgress)
case .Ended:
if gestureRecognizer.velocityInView(self.navigationController!.view).x & 0 {
self.interactionController?.finishInteractiveTransition()
self.interactionController?.cancelInteractiveTransition()
self.interactionController = nil
self.interactionController?.cancelInteractiveTransition()
self.interactionController = nil
在Begin中,pan手势一开始执行就初始化出UIPercentDrivenInteractiveTransition对象,并作为值赋给属性
. interactionController。
如果在第一个view controller就设定一个push(在早先定义的一个segue),在第二个view controller的时候就设定一个pop。
在navigation controller的push或者pop的时候则触发NavigationControllerDelegate的返回self.interactionController对象的方法。
Changed,在这个方法中根据手势移动的距离让动画移动不同的距离。这里apple已经替我们做了很多。
Ended,这里你会看到手势的移动速度。如果是正则transition结束,如果是负则取消。同时,把interactionController值设置为nil。
default,如果是其他的状态就直接取消trnasition并把interactionController值设置为nil。
运行程序,在屏幕上左右移动你的手指看看效果吧!
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致使用StoryBoard做iOS UI界面跳转
视频太卡?试试切换线路
是否提供源码:是课程等级:初级课程适合人群:有一定Xcode、Swift基础的开发者
用iOS Storyboard做界面跳转设计页面视频教程,主讲storyboard,storyboard跳转,Xcode storyboard,storyboard xib,main.storyboard,学会用Storyboard设计用户界面,实现iOS界面跳转
使用nib文件做iOS界面设计视频教程,介绍nib文件,iOS nib文件,Xcode nib文件,学习用nib文件iOS界面设计器设计iOS界面
在iOS界面间传递数据视频教程,主讲iOS多个界面,iOS界面传值,iOS界面跳转,学习iOS多个界面间传值数据实现
只有成为VIP会员才能提问&回复,快吧!如果你还没有账号你可以一个账号。
添加新技术问题
课程 [用iOS Storyboard做界面跳转设计页面]
中已存在问题
添加新技术问题
问题描述越详细,被解答的速度越快
有新回答时请邮件提醒我
着急,拜托快点
不急,慢慢解决
关联课程 [用iOS Storyboard做界面跳转设计页面]
服务热线:400-678-8266

我要回帖

更多关于 swift cell 跳转页面 的文章

 

随机推荐