自定义转场动画

UIViewControllerUI的转场

UIViewController类 有个 transitioningDelegate属性,也就是说所有该子类都可以自定义转场.下面是 UIViewController.h的代码

@protocol UIViewControllerTransitioningDelegate;

@interface UIViewController(UIViewControllerTransitioning)

@property (nullable, nonatomic, weak) id  transitioningDelegate NS_AVAILABLE_IOS(7_0);

@end

进入UIViewControllerTransitioning.h可以看到如下代码:


@class UIPresentationController;

@protocol UIViewControllerTransitioningDelegate 

@optional
- (nullable id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;

- (nullable id )animationControllerForDismissedController:(UIViewController *)dismissed;

- (nullable id )interactionControllerForPresentation:(id )animator;

- (nullable id )interactionControllerForDismissal:(id )animator;

- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0);

@end

注意: UIPresentationController是直接继承自NSObject对象,而不是UIViewController.

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIPresentationController : NSObject 

UIViewControllerAnimatedTransitioning协议里声明 两个方法:

// This is used for percent driven interactive transitions, as well as for
// container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id )transitionContext;
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
// 大意是: 如果转场是interactive 而不是percentDriven 转场,这个方法只能是空操作(无操作指令); 也就是时候 只有是percentDriven转场时该方法才被调用
- (void)animateTransition:(id )transitionContext;

UINavigationController 的转场

UINavigationControllerDelegate 提供了两个转场的代理方法

- (nullable id )navigationController:(UINavigationController *)navigationController
                          interactionControllerForAnimationController:(id ) animationController NS_AVAILABLE_IOS(7_0);

- (nullable id )navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

你可能感兴趣的:(自定义转场动画)