zoukankan      html  css  js  c++  java
  • iOS 自定义viewController 切换 (modal)

    自己写的一个modal 显示 ,为了解决 modal显示不能全屏的问题 代码如下:

    类 实现动画协议

    #import <Foundation/Foundation.h>

    @interface TransitionDelegate : NSObject

    @property (nonatomic, weak) UIViewController *delegate;

    • (id)initWithDelegate:(UIViewController *)delegate;
    • (void)presentViewController:(UIViewController *)modalViewController;
      @end

    #import "TransitionDelegate.h"
    #import "AnimatedTransitioning.h"
    #import "AnimatedTransitioningDismiss.h"

    @implementation TransitionDelegate
    //===================================================================
    // - UIViewControllerTransitioningDelegate
    //===================================================================

    • (id)initWithDelegate:(UIViewController *)delegate
      {
      self = [super init];
      if (self) {
      self.delegate = delegate;
      }
      return self;
      }

    • (void)presentViewController:(UIViewController *)modalViewController
      {
      modalViewController.modalPresentationStyle = UIModalPresentationCustom;
      modalViewController.transitioningDelegate = self;
      [self.delegate presentViewController:modalViewController animated:YES completion:^{
      }];
      }

    • (id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
      id animator;
      animator = [[AnimatedTransitioning alloc] init];
      return animator;
      }

    • (id )animationControllerForDismissedController:(UIViewController *)dismissed {

      id animator;
      animator = [[AnimatedTransitioningDismiss alloc] init];
      return animator;
      }

    @end

    类 实现显示动画

    #import <Foundation/Foundation.h>

    @interface AnimatedTransitioning : NSObject

    @end

    #import "AnimatedTransitioning.h"

    @implementation AnimatedTransitioning

    //===================================================================
    // - UIViewControllerAnimatedTransitioning
    //===================================================================

    • (NSTimeInterval)transitionDuration:(id )transitionContext {
      return 0.25f;
      }

    • (void)animateTransition:(id )transitionContext {

      UIView *inView = [transitionContext containerView];
      UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
      UIViewController *fromVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

      [inView addSubview:toVC.view];

      CGRect screenRect = [[UIScreen mainScreen] bounds];
      [toVC.view setFrame:CGRectMake(0, screenRect.size.height, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];

      [UIView animateWithDuration:0.25f
      animations:^{

                         [toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
      

    }

    @end

    类 实现dismiss动画

    import <Foundation/Foundation.h>

    @interface AnimatedTransitioningDismiss : NSObject

    @end

    #import "AnimatedTransitioningDismiss.h"

    @implementation AnimatedTransitioningDismiss

    • (NSTimeInterval)transitionDuration:(id)transitionContext
      {
      return 0.25f;
      }

    • (void)animateTransition:(id)transitionContext
      {
      UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

      CGRect initialFrame = [transitionContext initialFrameForViewController:fromVC];

      CGRect finalFrame = CGRectOffset(initialFrame, 0, CGRectGetHeight(transitionContext.containerView.frame));

      UIViewAnimationOptions opts = UIViewAnimationOptionCurveLinear;

      [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:opts animations:^{
      fromVC.view.frame = finalFrame;
      } completion:^(BOOL finished) {
      [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
      }];
      }

    @end

    调用方式 [self.transitioningDelegate presentViewController:sns];

  • 相关阅读:
    TCP三次握手和四次挥手
    shell脚本进阶
    shell脚本编程基础
    centos6/7通用查看系统版本
    OSI与TCP/IP网络模型分层
    RAID基础知识总结
    yum的初步了解与使用
    Red Hat Enterprise Linux 官方正式版镜像下载
    MYSQL的基本使用,以及错误代码的意思
    人生第一次研读MFC截图工具的笔记心得
  • 原文地址:https://www.cnblogs.com/xlliang/p/4186630.html
Copyright © 2011-2022 走看看