Core Animation - 发光的太阳(附高效设置图片圆角和变圆的方法)

先看下动画效果:
这里写图片描述

这里使用粒子动画,加上基础动画组合实现,还有一些绘图的方法,代码注释写的很清楚,看下面代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor grayColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(0, 0, 100, 100);
    btn.center = self.view.center;
    btn.layer.cornerRadius = 50;
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(good:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = [self circleImageSomeAngle:[UIImage imageNamed:@"zbar.jpg"]];
    [self.view addSubview:imageView];
}

- (void)good:(UIButton *)btn
{
    CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
    //发射器的尺寸大小
    [emitterLayer setEmitterSize:CGSizeMake(CGRectGetWidth(btn.frame), CGRectGetHeight(btn.frame))];
    //发射器的中心
    emitterLayer.emitterPosition = CGPointMake(btn.bounds.size.width / 2, btn.bounds.size.height / 2);
    //从发射器边缘发出
    emitterLayer.emitterMode = kCAEmitterLayerOutline;
    //发射器形状,圆,粒子在圆形范围发射
    emitterLayer.emitterShape = kCAEmitterLayerCircle;
    [btn.layer addSublayer:emitterLayer];
    
    CAEmitterCell *emitterCell = [[CAEmitterCell alloc]init];
    //设置一个关键字来查找这个对象
    [emitterCell setName:@"sunShine"];
    //设置内容
    emitterCell.contents = (__bridge id _Nullable)([self creatImage].CGImage);
    //设置出生率
    emitterCell.birthRate = 0;
    //设置生命周期
    emitterCell.lifetime = 0.4;
    //设置变透明的速度
    emitterCell.alphaSpeed = -2;
    //设置速率
    emitterCell.velocity = 200;
    //设置速率容差
    emitterCell.velocityRange = 200;
    //粒子单元数组,用来放发射粒子
    emitterLayer.emitterCells = @[emitterCell];
    
    
    CABasicAnimation *emitterBasic = [CABasicAnimation animationWithKeyPath:@"emitterCells.sunShine.birthRate"];
    //通过基础动画设置出生率,从有到无
    emitterBasic.fromValue = [NSNumber numberWithFloat:100000];
    emitterBasic.toValue = [NSNumber numberWithFloat:0];
    //因为存在生命周期,所以持续时间可以不给
    emitterBasic.duration = 0;
    //设置向外加速
    emitterBasic.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [emitterLayer addAnimation:emitterBasic forKey:@"good"];
    
}
//自己来画一张纯色图片
- (UIImage *)creatImage
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef text = UIGraphicsGetCurrentContext();
    //设置填充色
    CGContextSetFillColorWithColor(text, [UIColor yellowColor].CGColor);
    //设置填充区域
    CGContextFillRect(text, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [self circleImage:image];
    
}

//给图片变圆的高效方法
- (UIImage *)circleImage:(UIImage *)image
{
    UIGraphicsBeginImageContext(image.size);
    CGContextRef text = UIGraphicsGetCurrentContext();
    //设置边线宽
    CGContextSetLineWidth(text, 1);
    //设置边线条颜色
    CGContextSetStrokeColorWithColor(text, [UIColor blackColor].CGColor);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    //画一个圆
    CGContextAddArc(text, image.size.width / 2, image.size.height / 2, image.size.width / 2, 0, M_PI * 2, NO);
    //截取圆的部分
    CGContextClip(text);
    //在圆区域上画图
    CGContextDrawImage(text, rect, image.CGImage);
    UIImage *circlrImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return circlrImage;
    
}
//给图片加圆角的高效方法,同时还能设定具体哪个角是圆角

- (UIImage *)circleImageSomeAngle:(UIImage *)image
{
    //第二个参数为透明与否,第三个参数设置分辨率和屏幕一样
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);
    CGContextRef text = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(50, 50)];
    CGContextAddPath(text, maskPath.CGPath);
    CGContextClip(text);
    
    //下面注视的方法和CGContextDrawImage都可以,具体区别有知道的可以发表在评论处
//    [image drawInRect:rect];
//    CGContextDrawPath(text, kCGPathFillStroke);
    
    CGContextDrawImage(text, rect, image.CGImage);
    
    UIImage *circlrImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return circlrImage;
    
}

Demo下载地址:https://github.com/codeliu6572/Sunshine

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值