基于模态跳转
新建个项目
new一个新的vc扔在一边先不去管它好吧
现在之前的那个VC中放个按钮添加个target代码就不写了
下面是target代码:
-(void)next:(UIButton*)sender
{
NextViewController* vc = [NextViewController new];
vc.transitioningDelegate = self;
[self presentViewController:vc animated:YES completion:^{
}];
}
然后到我们新建的vc中写点东西,基本跟前一个VC没差
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
UIButton* backVC = [UIButton buttonWithType:UIButtonTypeCustom];
backVC.frame = CGRectMake(129, 250, 100, 44);
backVC.layer.borderWidth = 1;
[backVC setTitle:@"backVC" forState:UIControlStateNormal];
[backVC setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backVC addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backVC];
// Do any additional setup after loading the view.
}
-(void)back:(UIButton*)sender{
[self dismissViewControllerAnimated:YES completion:^{}];
}
效果:
然后主要就是要把这个过场动画给改了
注意这两个方法:
-(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
-(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
这两个返回一个管理过场动画的对象,一个进一个回
先不管这个点话,新建一个类
MJAnimation基于NSObject。
在头文件中加个枚举用来选取跳转类型嘛
typedef NS_ENUM(NSInteger, AnimationType) {
AnimationTypeValue1,
AnimationTypeValue2,
};
@interface MJAnimation : NSObject<UIViewControllerAnimatedTransitioning,CAAnimationDelegate>
//可以理解为上下文
@property (strong , nonatomic) id < UIViewControllerContextTransitioning > transitionContext;
@property (assign , nonatomic) AnimationType type;
//判断是进是出
@property (assign ,nonatomic) BOOL isPush;
//你猜。。。。。
@property (assign , nonatomic) CGRect circleCenterRect;
实现文件
#import "MJAnimation.h"
#define kWindowH [UIScreen mainScreen].bounds.size.height
#define kWindowW [UIScreen mainScreen].bounds.size.width
@implementation MJAnimation
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.5f;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
self.transitionContext = transitionContext;
UIViewController* toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
if (self.type == AnimationTypeValue1) {
[self AnimationAnimationTypeValue1:transitionContext andToVC:toVC andFromVC:fromVC];
}else if (self.type == AnimationTypeValue2)
{
[self AnimationAnimationTypeValue2:transitionContext andToVC:toVC andFromVC:fromVC];
}
}
#pragma mark - CABasicAnimation的Delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
[self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
[self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
}
#pragma mark -- AnimationAnimationTypeValue1
-(void)AnimationAnimationTypeValue1:(id<UIViewControllerContextTransitioning>)transitionContext andToVC:(UIViewController*)toVC andFromVC:(UIViewController*)fromVC
{
UIBezierPath *smallCircle = [UIBezierPath bezierPathWithOvalInRect:_circleCenterRect];
CGFloat centerX = self.circleCenterRect.origin.x + (self.circleCenterRect.size.width/2);
CGFloat centerY = self.circleCenterRect.origin.y - (self.circleCenterRect.size.height/2);
CGFloat circleR1 = (kWindowW - centerX) > centerX ? (kWindowW - centerX) : centerX;
CGFloat circleR2 = (kWindowH - centerY) > centerY ? (kWindowH - centerY) :centerY;
CGFloat radius = sqrt(circleR1 * circleR1 + circleR2 * circleR2);
UIBezierPath *largeCircle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.circleCenterRect, -radius, -radius)];
if (_isPush) {
[[transitionContext containerView] addSubview:toVC.view];
CAShapeLayer* shapeLayer = [CAShapeLayer layer];
toVC.view.layer.mask = shapeLayer;
shapeLayer.path = largeCircle.CGPath;
CABasicAnimation* shapeLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
shapeLayerAnimation.fromValue = (__bridge id _Nullable)(smallCircle.CGPath);
shapeLayerAnimation.toValue = (__bridge id _Nullable)(largeCircle.CGPath);
shapeLayerAnimation.duration = [self transitionDuration:transitionContext ];
shapeLayerAnimation.delegate = self;
[shapeLayer addAnimation:shapeLayerAnimation forKey:@"path"];
}else
{
[[transitionContext containerView] addSubview:toVC.view];
[[transitionContext containerView] sendSubviewToBack:toVC.view];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = smallCircle.CGPath;
fromVC.view.layer.mask = shapeLayer;
CABasicAnimation *shapeLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
shapeLayerAnimation.fromValue = (__bridge id _Nullable)(largeCircle.CGPath);
shapeLayerAnimation.toValue = (__bridge id _Nullable)((smallCircle.CGPath));
shapeLayerAnimation.duration = [self transitionDuration:transitionContext];
shapeLayerAnimation.delegate = self;
[shapeLayer addAnimation:shapeLayerAnimation forKey:@"path"];
}
}
之后去第一个vc中的那两个方法
//返回一个管理动画过渡的对象
-(nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
if (!self.animations) {
self.animations = [MJAnimation new];
}
self.animations.circleCenterRect = CGRectMake(0, 0, 10, 10);
self.animations.type = AnimationTypeValue1;
self.animations.isPush = YES;
return self.animations;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
if(!self.animations){
self.animations = [[MJAnimation alloc] init];
}
self.animations.type = AnimationTypeValue1;
self.animations.isPush = NO;
self.animations.circleCenterRect = CGRectMake(0, 0, 10, 10);
return self.animations;
}
然后之后再慢慢添加更多效果嘛。。。