• 之前用OC代码写过PUSH和POP的转场动画,闲来无事,将其转换成Swift语言,希望对大家有帮助,转载请注明。。。。如何实现PUSH和POP的转场动画?首先,创建一个NSObject的类,分别用来实现PUSH和POP的动画效果创建PUSH...

    之前用OC代码写过PUSH和POP的转场动画,闲来无事,将其转换成Swift语言,希望对大家有帮助,转载请注明。。。。

    如何实现PUSH和POP的转场动画?

    首先,创建一个NSObject的类,分别用来实现PUSH和POP的动画效果

    创建PUSH文件,实现扇形效果,代码如下:

    需要注意的是,代理的实现方法要完整

    var transitionContextT:UIViewControllerContextTransitioning?
        
        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.8
        }
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
            
            self.transitionContextT = transitionContext
            
            let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
            let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
            //不添加的话,屏幕什么都没有
            let containerView = transitionContext.containerView
            containerView.addSubview((fromVC?.view)!)
            containerView.addSubview((toVC?.view)!)
            
            let originRect:CGRect = CGRect.init(x: 0, y: 0, width: 50, height: 50)
            let maskStartPath = UIBezierPath.init(ovalIn: originRect)
            //OC中CGRectInset(originRect, -2000, -2000)的Swift用法:originRect.insetBy(dx: -2000, dy: -2000)
            let maskEndPath = UIBezierPath.init(ovalIn: originRect.insetBy(dx: -2000, dy: -2000))
            
            //创建一个CAShapeLayer来负责展示圆形遮盖
            let maskLayer = CAShapeLayer.init()
            //将他的path指定为最终的path,来避免在动画完成后回弹
            maskLayer.path = maskEndPath.cgPath
            
            toVC?.view.layer.mask = maskLayer
            
            let maskAnimation = CABasicAnimation.init(keyPath: "path")
            maskAnimation.fromValue = maskStartPath.cgPath
            maskAnimation.toValue = maskEndPath.cgPath
            maskAnimation.duration = self.transitionDuration(using: transitionContext)
            maskAnimation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
            maskAnimation.fillMode = kCAFillModeForwards
            maskAnimation.isRemovedOnCompletion = false
            maskAnimation.delegate = self
            maskLayer.add(maskAnimation, forKey: "path")
            
        }
        //MARK:----- CAAnimationDelegate
        func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
            self.transitionContextT?.completeTransition(!(self.transitionContextT?.transitionWasCancelled)!)
            //去除mask
            self.transitionContextT?.viewController(forKey: UITransitionContextViewControllerKey.from)?.view.layer.mask = nil;
            self.transitionContextT?.viewController(forKey: UITransitionContextViewControllerKey.to)?.view.layer.mask = nil;
        }

    然后,同理创建POP文件,实现弹跳的效果,代码如下:

    var transitionContext:UIViewControllerContextTransitioning?
        
        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.8
        }
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
            
            self.transitionContext = transitionContext
            
            let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
            let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
            //不添加的话,屏幕什么都没有
            let containerView = transitionContext.containerView
            containerView.addSubview((fromVC?.view)!)
            containerView.addSubview((toVC?.view)!)
            
            let durationN = self.transitionDuration(using: transitionContext)
            let screenBounds:CGRect = UIScreen.main.bounds
            let finalFrame:CGRect = transitionContext.finalFrame(for: toVC!)
            //OC中CGRectOffset(finalFrame, 0, -screenBounds.size.height)的Swift的用法:finalFrame.offsetBy(dx: 0, dy: -screenBounds.size.height)
            toVC?.view.frame = finalFrame.offsetBy(dx: 0, dy: -screenBounds.size.height)

            //添加动画,有弹跳的效果,参数:usingSpringWithDamping的范围为0.0f到1.0f,数值越小「弹簧」的振动效果越明显,当设置为1.0时,就不弹跳
            //toVC?.view.frame = finalFrame
            //transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            UIView.animate(withDuration: durationN, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.curveLinear, animations: {() -> Void in
                //print("11111111")
                toVC?.view.frame = finalFrame
            }, completion: ({(Bool) -> Void in
                //print("22222222")
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }))
        }

    最后在需要使用跳转动画的地方添加self.navigationController?.delegate = self代理方法,并实现,代码如下:

    //MARK:-----UINavigationControllerDelegate
        func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            if operation==UINavigationControllerOperation.push {
                return BHPopAnimation()
            }
            else if operation==UINavigationControllerOperation.pop{
                return BHPushAnimation()
            }
            return nil
        }

    最后附上源代码,如果有问题请留言:https://github.com/hbblzjy/SwiftPushAndPopDemo


    展开全文
  • Swift 使用presentViewController 跳转页面后显示黑屏 原因:storyboard 制作页面 和 纯代码页面,需要使用两种不同方法进行页面跳转 Storyboard 界面:使用代码跳转 let sb = UIStoryboard(name: "Main", ...
  • 页面动画的跳转.zip 相关代码 import UIKit class ViewController: UIViewController,UINavigationControllerDelegate,UIViewControllerAnimatedTransitioning,UIViewControllerInteractiveTrans...
  • swift 页面跳转 2019-07-16 16:18:57
    左右跳转: //实例化一个将要跳转的 viewController let secondView = SecondViewController() //跳转 self.navigationController?.pushViewController(secondView , animated: true) 返回上一级: self....
  • 平时大家跳转页面,我们模态跳转到某个页面使用 [self presentViewController: animated: completion:] 返回时使用对应的返回方法  [self ...push跳转页面使用  [self.navigationControlle
  • Swift的开发中,实现页面之间的跳转我们都是通过在UIViewController来实现其push方法或者present方法来跳转页面的(即控制器之间的跳转切换),但是往往在开发中我们经常会使用MVC的架构来将页面进行模块化的封装,...
  • 方法一:  @objc fileprivate func loginBtnAction(sender: UIButton) {  let bYTabBar = BYMainTabBarController() // self.present(bYTabBar, animated: true, completion: nil)
  • 今天没事研究了一下跳转方式;发现push还可以有莫泰的效果,甚至其他更多的效果;废话不多说,上代码: //方法一:模态跳转,前提是利用navigationcontroller这个容器 // UserManagerViewController *...
  • OC、swift混编中的相互跳转和传值
  • 首先要在appDelegate里添加导航视图控制器navigationColler func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?...
  • 跳转Push时的Controller为 UIViewControllerPresent时的Controller为 UINavigationController使用 Storyboard Segue 方式:在Storyboard中设置Segue的Identifier,以及Kind然后使用代码:performSegue(withIdentifier...
  • Swift和OC控制器互相跳转 2017-03-24 10:11:35
    碰到一个swift的项目,博主要用OC来进行重构,但是考虑到时间的问题,里面几大功能模块打算直接抑制到项目里,这里要考虑Swift和OC之间界面的互相跳转,也是混编。 Swift中使用OC使用桥接文件,可以查看...
  • swift push和pop 2018-02-06 15:54:37
    push跳转到指定页面 self.navigationController?.pushViewController(FoundNewsViewController(), animated: true) pop返回上一个页面 self.navigationController?.popViewController(animated: true)
  • swift 中 pushViewController不执行 文档解释是: If the view controller or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This ...
  • Swift推送通知跳转页面 2019-06-16 12:13:09
    2019独角兽企业重金招聘Python工程师标准>>> ...
  • Swift实现IOS界面的跳转 2017-09-28 15:40:33
    IOS开发中界面跳转有两种方式,上下跳转和左右跳转。 上下跳转_TO: [objc] view plain copy  print? let secondViewController = SecondViewController()  self....
  • 如果不需要以编程方式关闭模式,那么NavigationLink 是最优的选择。但如果需要编写更负责的控制逻辑,那么需要以编程的方式来控制推送和弹出了。 例如,我们想在界面中增加一个按钮,希望通过这个按钮来控制返回的...
  • swift中通过 storyboard或者编程的方法 来实现一个 UITabBarController 进行各个UIViewController进行切换 相关阅读:http://makeapppie.com/2014/09/09/swift-swift-using-tab-bar-controllers-in-swift/ 前面...
  • 1.[self.navigationController pushViewController:newC... //跳转到下一页面 2.[self.navigationController popViewControllerAnimated:YES]; //返回上一页面 3.[self.navigationController popToRootViewCo
1 2 3 4 5 ... 20
收藏数 1,431
精华内容 572