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

posted on 2014-12-26 14:03  孤独与酒  阅读(392)  评论(0编辑  收藏  举报

导航