• 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];

  • 相关阅读:
    为什么单片机程序中会有延时程序加入
    20145238 《信息安全系统设计基础》课程总结
    20145238-荆玉茗 《信息安全系统设计》第14周学习总结
    20145238-荆玉茗 《信息安全系统设计基础》第13周学习总结
    20145238-荆玉茗 《信息安全系统设计基础》第十二周学习总结
    补:第五周实验楼实践
    20145224&20145238《信息安全系统设计基础》实验三
    20145238 《信息安全系统设计基础》第十一周学习总结
    20145238《信息安全系统设计基础》第十周学习总结
    20145224&20145238《信息安全系统设计基础》实验五
  • 原文地址:https://www.cnblogs.com/xlliang/p/4186630.html
Copyright © 2020-2023  润新知