简介
在iOS 7
之前,我们只能使用系统提供的转场效果,iOS 7
之后苹果开放了相关API
允许我们对转场效果进行全面定制,它是 以协议的方式开放了自定义转场的API
,协议的好处是不再拘泥于具体的某个类,只要是遵守该协议的对象都能参与转场,非常灵活,这样对自定义转场动画以及交互手段的支持带来了无限可能。
动画控制器
新建一个继承NSObject
的类,枚举转场类型,遵守UIViewControllerAnimatedTransitioning
协议,实现协议方法,自定义转场的动画效果。
typedef NS_ENUM(NSUInteger, NSTransitionStyle) {
NSTransitionStylePush,
NSTransitionStylePop,
NSTransitionStylePresent,
NSTransitionStyleDismiss
};
@interface CustomTransition : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) NSTransitionStyle style;
@end
@implementation CustomTransition
#pragma mark - UIViewControllerAnimatedTransitioning
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
switch (self.style) {
case NSTransitionStylePush:
[self pushAnimation:transitionContext];
break;
case NSTransitionStylePop:
[self popAnimation:transitionContext];
break;
case NSTransitionStylePresent:
[self presentAnimation:transitionContext];
break;
case NSTransitionStyleDismiss:
[self dismissAnimation:transitionContext];
break;
default:
break;
}
}
- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
return 1.0f;
}
#pragma mark - Animation
/**
push动画
@param transitionContext 转场上下文
*/
- (void)pushAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
}
/**
pop动画实现
@param transitionContext 转场上下文
*/
- (void)popAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
}
/**
present动画实现
@param transitionContext 转场上下文
*/
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
}
/**
dismiss动画实现
@param transitionContext 转场上下文
*/
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
}
@end
转场环境
提供转场中需要的数据,遵守UIViewControllerContextTransitioning
协议。
- 获取控制器
//获取转场相关的两个控制器 iOS7的API
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
- 获取视图
//iOS8之后使用
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
- 获取容器
//获取容器视图
UIView *containerView = [transitionContext containerView];
转场代理
- 导航转场
From控制器
遵守导航代理<UINavigationControllerDelegate>
// 别忘记设置代理对象
self.navigationController.delegate = self;
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
CustomTransition *transition = [[CustomTransition alloc] init];
switch (operation) {
case UINavigationControllerOperationPush:
transition.style = NSTransitionStylePush;
break;
case UINavigationControllerOperationPop:
transition.style = NSTransitionStylePop;
break;
default:
break;
}
return transition;
}
- 模态转场
From控制器
遵守模态代理<UIViewControllerTransitioningDelegate
>
// 别忘记设置present视图控制器代理对象为From视图控制器
nextVC.transitioningDelegate = self;
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
CustomTransition *transition = [[CustomTransition alloc] init];
transition.style = NSTransitionStylePresent;
return transition;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
CustomTransition *transition = [[CustomTransition alloc] init];
transition.style = NSTransitionStyleDismiss;
return transition;
}
注意事项
- 目标视图
toView
要添加到容器视图containerView
里。 - 转场类型不同对应的目标视图
toView
和当前视图fromVC
也不同。