iOS 圆形扩散跳转转场动画

最近写代码的时候得到了一个需求,很多APP上都遇见过,就是圆形扩散的转场动画。今天分享给大家,看似很复杂的效果,实际上非常简单。

先上图:


转场动画.gif

首先我们需要两个控制器:ViewController、SecondViewController。我们从ViewController跳转到SecondViewController。

这里我们先展示SecondViewController控制器的内容:

#import "SecondViewController.h"

@interface SecondViewController ()

/// 图片
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.imageView];
}

#pragma mark - Get方法
-(UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
        _imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    }
    
    return _imageView;
}

@end

只有一张图片,是一个很简单的控制器
接下来展示ViewController:

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController () <CAAnimationDelegate>

/// 按钮
@property (nonatomic, strong) UIButton *presentButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.presentButton];
}

#pragma mark - 点击事件
-(void)persentAction:(UIButton *)button {
    // 充当渐变的背景(可以用自定义view代替)
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    bgImageView.frame = self.view.frame;
    [self.view addSubview:bgImageView];
    
    UIBezierPath *maskStartBP =  [UIBezierPath bezierPathWithOvalInRect:button.frame];
    CGFloat radius = [UIScreen mainScreen].bounds.size.height - 100;
    UIBezierPath *maskFinalBP = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(button.frame, -radius, -radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = maskFinalBP.CGPath;
    maskLayer.backgroundColor = (__bridge CGColorRef)([UIColor whiteColor]);
    bgImageView.layer.mask = maskLayer;
    CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAnimation.fromValue = (__bridge id)(maskStartBP.CGPath);
    maskLayerAnimation.toValue = (__bridge id)((maskFinalBP.CGPath));
    // 时间
    maskLayerAnimation.duration = 5.f;
    maskLayerAnimation.delegate = self;
    [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}

/// 结束事件
-(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
    SecondViewController *viewController = [[SecondViewController alloc] init];
    
    [self presentViewController:viewController animated:NO completion:nil];
}

/// 开始事件
-(void)animationDidStart:(CAAnimation *)anim {
    NSLog(@"开始事件");
}

#pragma mark - Get方法
-(UIButton *)presentButton {
    if (!_presentButton) {
        _presentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _presentButton.frame = CGRectMake(0, 0, 100, 40);
        _presentButton.center = self.view.center;
        _presentButton.backgroundColor = [UIColor whiteColor];
        [_presentButton setTitle:@"跳转" forState:UIControlStateNormal];
        [_presentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_presentButton addTarget:self action:@selector(persentAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _presentButton;
}

@end

其实在做转场动画的时候,我添加了一个“假”的视图,当动画结束的时候进行跳转。效果就显得很酷炫了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,041评论 3 119
  • 好像记事以来,最近的一场发烧是上小学的时候。自己的体质一直都不是易生小病的属性。这次从年三十开始低烧,烧到初一晚上...
    aurora硬硬同学阅读 1,313评论 0 0
  • 我并不知道要如何写这封信,如同不能与你好好做道别。回首过去漫长的时光里,我们不断地犯错,不断的错过。你最喜欢我...
    37s阅读 3,130评论 0 0
  • 卡沙是一条围巾。 当他还是一团毛线时,他就知道,自己将来会被眼前这个,笑起来眉眼弯弯的女孩送给男朋友。 从货柜上的...
    瑜音荛阅读 3,434评论 16 28
  • 笑话是很早就闹出去了,因为我又迎上你那惊奇的目光。“真的这么想啊?”说这话时,你的眼睛特别的明亮,它闪着细细密密的...
    壮小梦阅读 1,729评论 0 1