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

  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/xlliang/p/4186630.html
Copyright © 2011-2022 走看看