好久没更新了,最近一直比较忙,也是有点懒了没更新,实在不好意思,学习的步伐不能停下啊。
好了,废话不多说今天来聊一聊iOS的自定义转场的实现。
简述
iOS的新页面的弹出方式分为两种Push&Pop和Present&Dismiss,转场方式的自定义也主要针对这两种方式进行。其中Push&Pop方式需要针对Push和Pop分别定义动画,也就是说需要分别创建动画文件。而Present&Dismiss方式的话只需要一个即可。
Push&Pop方式
动画文件
首先来看一下动画文件的实现,
1、先创建一个类继承自NSObject
2、遵守协议UIViewControllerAnimatedTransitioning
3、接下来主要实现三个方法即可
@interface PushTransition : NSObject
@property (nonatomic,strong) id transitionContext;//专场上下文,用来协调,from和to的页面
@property (nonatomic,strong) UIViewController *fromViewController;//push操作主要针对fromViewController做动画
@end
First
主要用来返回动画执行时间,即为push动画的时间,直接return一个时间即可,单位为秒
- (NSTimeInterval)transitionDuration:(nullable id )transitionContext
{
return 2;
}
Second
下面这个方法就主要涉及到动画的实现了,需要对FromViewController的view做动画,来实现push的动画效果,主要解释已在注释里面写的很清楚了
- (void)animateTransition:(id )transitionContext
{_transitionContext = transitionContext;//首先记录transitionContext,下面的方法会用到
//我们主要通过这个transitionContext来获取需要做动画的fromViewController和toViewController,fromView和toView
//通过viewControllerForKey:这个方法来获取,有两个key来获取相应的Controller
// Currently only two keys are defined by the system:
// UITransitionContextToViewControllerKey 获取要出现的Controller
// UITransitionContextFromViewControllerKey 获取要消失的Controller
// Currently only two keys are defined by the system:
// UITransitionContextToViewKey //获取要出现的view即上面toViewController的View
// UITransitionContextFromViewKey //获取要出现的view即fromViewController的View
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//拿到ContainerView
UIView *containerView = [transitionContext containerView];
//把fromView和toView都加到containerView中,在容器中是完成动画
[containerView addSubview:toView];
[containerView addSubview:fromView];
//CATransform3D类型的四维仿射变换矩阵,并且提供预置好的进行旋转、变形之后的仿射变换矩阵,m34来控制透视效果,有兴趣的话可以在自己了解一下
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/500.0;
fromView.layer.transform = transform;
fromView.layer.anchorPoint = CGPointMake(0, 0.5);
fromView.layer.position = CGPointMake(0, CGRectGetMidY(fromView.frame));
//动画变化
[UIView animateWithDuration:2 animations:^{
fromView.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
} completion:^(BOOL finished) {
// This must be called whenever a transition completes (or is cancelled.)
// Typically this is called by the object conforming to the
// UIViewControllerAnimatedTransitioning protocol that was vended by the transitioning
// delegate. For purely interactive transitions it should be called by the
// interaction controller. This method effectively updates internal view
// controller state at the end of the transition.
//动画完成后必须调用
[_transitionContext finishInteractiveTransition];
[_transitionContext completeTransition:YES];
}];
}
Pop的话,其实跟Push差不多这里就只贴代码不做过多解释了
- (NSTimeInterval)transitionDuration:(id)transitionContext{
return 2;
}
- (void)animateTransition:(id)transitionContext{
_transitionContext = transitionContext;
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[fromViewController.view.layer removeAllAnimations];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:fromViewController.view];
[containerView addSubview:toViewController.view];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/500.0;
toViewController.view.layer.transform = transform;
toViewController.view.layer.anchorPoint = CGPointMake(0, 0.5);
toViewController.view.layer.position = CGPointMake(0, CGRectGetMidY(fromViewController.view.frame));
toViewController.view.layer.transform = CATransform3DMakeRotation(-M_PI_2, 0, 1, 0);
[UIView animateWithDuration:2 animations:^{
toViewController.view.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {
[_transitionContext finishInteractiveTransition];
[_transitionContext completeTransition:YES];
}];
}
下面我们来介绍一下present和dismiss
这里先简单提一下代理,OC中的代理,实在是十分好用,为了将我们的代码耦合性降低。我们可以单独生成一个类,来实现动画,这样的话可以精简Controller里面的代码,另外是代码的复用性进一步增加。动画文件可以单独放在任何地方应用。
下面我们来实现.h
//首先遵循以下两个协议,要实现自定义转场必须遵守的
@interface ModalTransition : NSObject
@property (nonatomic, assign) BOOL isPresent;//记录是Present还是Dimiss
@end
.m实现,这里我只是很简单的写了一个弹簧动画,对于动画方式大家可以自由发挥,写出更加炫酷的效果
@implementation ModalTransition
//下面两个代理方法要返回一个遵循UIViewControllerAnimatedTransitioning协议的对象,返回self即可。记录一下是Present还是Dimiss。主要讲解我还放在注释里面
- (id)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
_isPresent = YES;
return self;
}
- (id)animationControllerForDismissedController:(UIViewController *)dismissed{
_isPresent = NO;
return self;
}
//返回动画时间
- (NSTimeInterval)transitionDuration:(id)transitionContext{
return 1.0;
}
//根据_isPresent来执行不同的动画
- (void)animateTransition:(id)transitionContext{
_isPresent ? [self animationForPresentedView:transitionContext] : [self animationForDismissView:transitionContext];
}
//自定义方法,Present的动画实现
-(void)animationForPresentedView:(id)transitionContext{
UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//注意添加的次序
[[transitionContext containerView] addSubview:fromView];
[[transitionContext containerView] addSubview:presentedView];
presentedView.transform = CGAffineTransformTranslate(presentedView.transform, 0, 667);
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
presentedView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
//完成后记得调用completeTransition:方法
[transitionContext completeTransition:YES];
}];
}
//自定义方法,Present的动画实现
-(void)animationForDismissView:(id)transitionContext{
UIView *dismissedView = [transitionContext viewForKey:UITransitionContextFromViewKey];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
[[transitionContext containerView] addSubview:toView];
//注意添加的次序
[[transitionContext containerView] addSubview:dismissedView];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
dismissedView.transform = CGAffineTransformTranslate(dismissedView.transform, 0, 667);
} completion:^(BOOL finished) {
//完成后记得调用completeTransition:方法
[transitionContext completeTransition:YES];
}];
}
@end
使用方法也标简单
PresentViewController *presentController = [[PresentViewController alloc]init];
presentController.modalTransitionStyle = UIModalPresentationCustom;
presentController.transitioningDelegate = _Transition;//_Transition即为上面冬瓜文件生成的对象,这样就可以使用自定义的present动画
[self presentViewController:presentController animated:YES completion:nil];
到这里基本就实现完成了,不管是Pop&Push还是Present&Dismiss你都可以实现任何你想要的炫酷效果(当然前提是你能把炫酷的动画优雅的实现出来),我这里只是抛砖引玉,期待你有更完美,漂亮的动画效果。