ios开发 方形到圆的动画_iOS 圆形扩散跳转转场动画

本文介绍了如何在iOS中创建一个从方形到圆形的动画效果,用于实现类似APP圆形扩散的转场过渡。通过示例代码详细展示了如何在两个控制器间添加动画,并在动画结束后进行视图的切换,达到酷炫的视觉效果。
摘要由CSDN通过智能技术生成

最近写代码的时候得到了一个需求,很多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 ()

/// 按钮

@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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值