//
//  TVOverlaySegue.m
//  OverlaySegueDemo
//
//  Created by zhoujinrui on 16/3/11.
//  Copyright 漏 2016骞?Maple. All rights reserved.
//

#import "TVOverlaySegue.h"
#import "TVOverlayAnimationController.h"
#import <objc/runtime.h>

static const char kTransitioningDelegateKey = 0;

@interface TVOverlayTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) TVOverlayAnimationController *animationController;

@end

@interface TVOverlaySegue ()

@property (nonatomic, strong, readonly) TVOverlayTransitioningDelegate *transitioningDelegate;

@end

@implementation TVOverlaySegue
@synthesize transitioningDelegate = _transitioningDelegate;

- (TVOverlayTransitioningDelegate *)transitioningDelegate
{
    if (!_transitioningDelegate)
    {
        _transitioningDelegate = [[TVOverlayTransitioningDelegate alloc] init];
        
        // 杞満浠g悊鐢熷懡鍛ㄦ湡涓庡脊鍑虹殑瑙嗗浘鎺у埗鍣ㄤ繚鎸佷竴鑷?        objc_setAssociatedObject(self.destinationViewController, &kTransitioningDelegateKey, _transitioningDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _transitioningDelegate;
}

- (void)perform
{
    self.destinationViewController.transitioningDelegate = self.transitioningDelegate;
    self.destinationViewController.modalPresentationStyle = UIModalPresentationCustom;
    self.destinationViewController.modalPresentationCapturesStatusBarAppearance = YES;
    
    [self.sourceViewController presentViewController:self.destinationViewController animated:YES completion:nil];
}

@end

@implementation TVOverlayTransitioningDelegate

- (TVOverlayAnimationController *)animationController
{
    if (!_animationController)
    {
        _animationController = [[TVOverlayAnimationController alloc] init];
    }
    return _animationController;
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.animationController.animationType = TVOverlayPresentationAnimation;
    return self.animationController;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.animationController.animationType = TVOverlayDismissalAnimation;
    return self.animationController;
}

@end
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com