自定义UIPresentationController

来源:互联网 发布:ubuntu解压tar.gz命令 编辑:程序博客网 时间:2024/05/16 07:12

首先设置设置需要modal的控制器的代理;并且设置他的present模式为自定义
比如:

vc.modalPresentationStyle = UIModalPresentationCustom;vc.transitioningDelegate = self;

实现UIViewControllerTransitioningDelegate的代理方法;可以写一个单例来包含他们。并且VC的代理设为[objcet shareObject];

/** 由返回的对象控制需要展示的控制器;*/- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{    return [[ZGPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];}/** 由返回的对象控制Presented时的动画;*/- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{    ZGAnimatedTransitioning *anim = [[ZGAnimatedTransitioning alloc] init];    anim.presented = YES;    return anim;}/** 由返回的控制器控制dismissed时的动画;*/- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{    ZGAnimatedTransitioning *anim = [[ZGAnimatedTransitioning alloc] init];    anim.presented = NO;    return anim;}

重写presentation方法的类ZGPresentationController

@implementation ZGPresentationController//- (CGRect)frameOfPresentedViewInContainerView//{////    return CGRectMake(0, 50, self.containerView.frame.size.width, self.containerView.frame.size.height - 100);//    return CGRectInset(self.containerView.bounds, 0, 100);//}- (void)presentationTransitionWillBegin{//    NSLog(@"presentationTransitionWillBegin");    self.presentedView.frame = self.containerView.bounds;    [self.containerView addSubview:self.presentedView];}- (void)presentationTransitionDidEnd:(BOOL)completed{//    NSLog(@"presentationTransitionDidEnd");}- (void)dismissalTransitionWillBegin{//    NSLog(@"dismissalTransitionWillBegin");}- (void)dismissalTransitionDidEnd:(BOOL)completed{//    NSLog(@"dismissalTransitionDidEnd");    [self.presentedView removeFromSuperview];}@end

重写AnimatedTransitioning方法的类ZGAnimatedTransitioning

#import <UIKit/UIKit.h>@interface ZGAnimatedTransitioning : NSObject <UIViewControllerAnimatedTransitioning>//一定要遵守这个协议@end
const CGFloat duration = 1.0;@implementation ZGAnimatedTransitioning#pragma mark - UIViewControllerAnimatedTransitioning- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{    return duration;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{    if (self.presented) {        UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];//        toView.layer.transform = CATransform3DMakeRotation(M_PI_2, 1, 1, 0);//        toView.y = -toView.height;        toView.x = toView.width;        [UIView animateWithDuration:duration animations:^{//            toView.y = 0;            toView.x = 0;//            toView.layer.transform = CATransform3DIdentity;        } completion:^(BOOL finished) {            [transitionContext completeTransition:YES];        }];    } else {        [UIView animateWithDuration:duration animations:^{            UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];//            fromView.y = -fromView.height;            fromView.x = -fromView.width;//            fromView.layer.transform = CATransform3DMakeRotation(M_PI_2, 1, 1, 0);        } completion:^(BOOL finished) {            [transitionContext completeTransition:YES];        }];    }}@end

到时候只要将这三个类封装,然后再调用我一开始提到的两句即可自定义一个modal了。具体的modal各种形态的修改在ZGAnimatedTransitioning和ZGPresentationController类中。

0 0
原创粉丝点击