自定义转场的类型
按照页面类型:1、present/dismiss 2、push/pop 3、tabbarSwitch
按照交互方式:1、交互式转场 2、非交互式转场
实现步骤
1、用自定义而非默认的转场 UIViewControllerTransitioningDelegate
2、转场过程动画 UIViewControllerAnimatedTransitioning
3、转场过程数据 UIViewControllerContextTrasitioning
4、手势驱动转场 UIViewControllerInteractiveTransitioning
5、转场协调 UIViewControllerTransitionCoordinator
其中1、2、3为必要步骤
自定义 Present/dismiss转场
vc有属性要不要将fromview移除,在modal时
vc.modalPresentationStyle=UIModalPresentationCustom;//custom不移除 fullscreen移除
1、@protocal UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController*)presented presentingController:(UIViewController*)presenting sourceController:(UIViewController*)source;
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController*)dismissed;
//注意:返回值是id<UIViewControllerAnimatedTransitioning> 就是说返回值需要满足UIViewControllerAnimatedTransitioning协议也就是步骤2
2、@protocal UIViewControllerAnimatedTransitioning
//转场时间
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;
//转场动画
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;
//注意参数transitionContext实现了UIViewControllerContextTransitioning协议,也就是步骤3
实例
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1; //转场时长1s
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
//获取ToViewController
UIViewController* toVC=[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView* toView=[transitionContext viewForKey:UITransitionContextToViewKey];
//获取当前上下文的view
UIView* containerView=[transitionContext containerView];
[containerView addSubview:toView];
//获取targetFrame并、且让toView的frame上平移targetFrame.size.height
CGRect targetFrame=[transitionContext finalFrameForViewController:toVC];
toView.frame=CGRectOffset(targetFrame,0, -targetFrame.size.height);
NSTimeInterval duration=[self transitionDuration:transitionContext];
[UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveLinear animations:^{
toView.frame=targetFrame;
}completion:^(BOOLfinished) {
//表示转场完成
[transitionContext completeTransition:YES];
}];
}
自定义push/pop转场
1、UINavigaitionControllerDelegate用自定义而非系统方式
////operation定义了是push还是pop操作
- (nullable id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC ;
注意:返回值实现了UIViewControllerAnimatedTransitioning协议
2、添加一个NSObject实例 实现UIViewControllerAnimatedTransitioning协议
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return1;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
UIViewController* toVC=[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromVC=[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContextcontainerView]addSubview:toVC.view];
toVC.view.alpha=0;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromVC.view.transform=CGAffineTransformMakeScale(0.1, 0.1);
toVC.view.alpha=1;
}completion:^(BOOLfinished) {
fromVC.view.transform=CGAffineTransformIdentity;
[transitionContextcompleteTransition:YES];
}];
}
//注意参数transitionContext实现了UIViewControllerContextTransitioning协议,也就是步骤3
TabBatController自定义转场
1、非交互式与交互式
//非交互式
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC
方法原理同上面两个不再介绍
//交互式
-(id<UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController*)tabBarController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController;
注意这里返回值需要实现UIViewControllerInteractiveTransitioning协议,参数animationController实现了UIViewControllerAnimatedTransitioning协议;
UIViewControllerInteractiveTransitioning协议包含
- (void)startInteractiveTransition:(id)transitionContext;
@optional
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) CGFloat completionSpeed;
@property(nonatomic, readonly) UIViewAnimationCurve completionCurve;
#else
- (CGFloat)completionSpeed;
- (UIViewAnimationCurve)completionCurve;
#endif
/// In 10.0, if an object conforming to UIViewControllerAnimatedTransitioning is
/// known to be interruptible, it is possible to start it as if it was not
/// interactive and then interrupt the transition and interact with it. In this
/// case, implement this method and return NO. If an interactor does not
/// implement this method, YES is assumed.
@property(nonatomic,readonly)BOOLwantsInteractiveStartNS_AVAILABLE_IOS(10_0);
现成的实现了UIViewControllerInteractiveTransitioning协议的类 UIPercentDrivenInteractiveTransition 用百分比实现交互式过程
@property (readonly) CGFloat duration;
@property(readonly)CGFloatpercentComplete;
@property (nonatomic,assign) CGFloat completionSpeed;
@property (nonatomic,assign) UIViewAnimationCurve completionCurve;
@property (nullable, nonatomic, strong)id <UITimingCurveProvider> timingCurve NS_AVAILABLE_IOS(10_0);
@property(nonatomic)BOOLwantsInteractiveStart NS_AVAILABLE_IOS(10_0);
- (void)pauseInteractiveTransition NS_AVAILABLE_IOS(10_0);
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)cancelInteractiveTransition;
- (void)finishInteractiveTransition;
步骤 弱引用TabBarviewController 添加手势 手势方法中处理
网友评论