iOS核心动画实现仿支付宝咻咻、雷达效果
2016-05-13 本文已影响3449人
Sean_Shi
今天闲余时间写了一个类似支付宝咻咻或者说雷达效果。望大神勿笑、写的粗糙。
更新了下文章,贴出Demo地址(已更新一波)
github
先上效果图吧:
ps:gif图有点抖~~~
雷达效果图
说下主要思路吧:
主要用到了CAShapeLayer和UIBezierPath的经典结合画出自己想要的图再结合CAAnimation将动画添加到layer上实现效果。
timingFunction:动画的运动轨迹,用于变化起点和终点之间的插值计算,即它决定了动画运行的节奏,比如是均 匀变化(相同时间变化量相同)还是先快后慢,先慢后快还是先慢再快再慢。
动画的开始与结束的快慢,有五个预置分别为:
kCAMediaTimingFunctionLinear
线性,即匀速
kCAMediaTimingFunctionEaseIn 先慢后快
kCAMediaTimingFunctionEaseOut 先快后慢
kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
kCAMediaTimingFunctionDefault 中间快
代码部分 ( 基本全部代码)
自定义一个view-- RadarAnimationView.h:
这里提供了一些属性、可以在外部进行设置。
//定义Block的目的是为了点击雷达中间按钮时调用,此处定义,方便以后使用。
typedef void(^RaderBtnBlock)();
typedef void(^RaderBtnSelectBlock)();
#import <UIKit/UIKit.h>
//雷达效果
@interface RadarAnimationView : UIView
/**
* 按钮图片
*/
@property (nonatomic ,strong) UIImage *image;
/**
* 按钮选中图片
*/
@property (nonatomic ,strong) UIImage *selectImage;
/**
* 波纹颜色
*/
@property (nonatomic,strong) UIColor *raderColor;
/**
* 按钮block 未选择状态
*/
@property (nonatomic,copy) RaderBtnBlock block;
/**
* 按钮block 选择状态
*/
@property (nonatomic,copy) RaderBtnSelectBlock selectBlock;
@end
RadarAnimationView.m实现部分
static const float timeinterval = 0.5;
#import <QuartzCore/QuartzCore.h>
#import "RadarAnimationView.h"
@interface RadarAnimationView ()
//按钮图片
@property (nonatomic,strong)UIButton *iamgeBtn;
@end
@implementation RadarAnimationView
- (void)drawRect:(CGRect)rect {
self.alpha = 0.5;
for (int i = 0; i < 20; i++) {
[self addAnimationDelay:i];
}
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_iamgeBtn = [[UIButton alloc] initWithFrame:self.bounds];
[_iamgeBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_iamgeBtn];
//设置默认波纹颜色
self.raderColor = [UIColor blueColor];
}
return self;
}
//按钮点击事件
-(void)imageBtnClick:(UIButton *)btn
{
if (btn.selected) {
NSLog(@"1");
//调用block
self.block(@"1");
}else
{
NSLog(@"2");
//调用block
self.selectBlock(@"2");
}
btn.selected = !btn.selected;
}
//重写setter方法
-(void)setImage:(UIImage *)image
{
_image = image;
[self.iamgeBtn setImage:image forState:0];
}
-(void)setSelectImage:(UIImage *)selectImage
{
_selectImage = selectImage;
[self.iamgeBtn setImage:selectImage forState:1];
}
-(void)setRaderColor:(UIColor *)raderColor
{
_raderColor = raderColor;
}
-(void)addAnimationDelay:(int)time
{
CGPoint centerPoint = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);
//使用贝塞尔画圆
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:10 startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.bounds;
shapeLayer.fillColor = self.raderColor.CGColor;
shapeLayer.opacity = 0.2;
shapeLayer.path = path.CGPath;
[self.layer insertSublayer:shapeLayer below:self.iamgeBtn.layer];
//雷达圈圈大小的动画
CABasicAnimation *basicAnimation = [CABasicAnimation animation];
basicAnimation.keyPath = @"path";
CGPoint center = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);
UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:0 endAngle:2 * M_PI clockwise:YES];
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:[UIScreen mainScreen].bounds.size.height startAngle:0 endAngle:2 * M_PI clockwise:YES];
basicAnimation.fromValue = (__bridge id _Nullable)(path1.CGPath);
basicAnimation.toValue = (__bridge id _Nullable)(path2.CGPath);
basicAnimation.fillMode = kCAFillModeForwards;
//雷达圈圈的透明度
CABasicAnimation *opacityAnimation = [CABasicAnimation animation];
opacityAnimation.keyPath = @"opacity";
opacityAnimation.fromValue = @(0.2);
opacityAnimation.toValue = @(0);
opacityAnimation.fillMode = kCAFillModeForwards;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[basicAnimation,opacityAnimation];
//动画间隔时间 这里的值和创建的动画个数需要计算好,避免最后一轮动画结束了,第一个动画好没有结束,出现效果差
group.duration = 10;
//动画开始时间
group.beginTime = CACurrentMediaTime() + (double)time/2;
//循环次数最大(无尽) HUGE
group.repeatCount = HUGE;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
group.delegate = self;
//指定的时间段完成后,动画就自动的从层上移除
group.removedOnCompletion = YES;
//添加动画到layer
[shapeLayer addAnimation:group forKey:nil];
}
@end
在VC中调用
//
#import "ViewController.h"
#import "RadarAnimationView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:23/255.0 green:26/255.0 blue:40/255.0 alpha:1];
RadarAnimationView *rader = [[RadarAnimationView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
rader.center = self.view.center;
rader.backgroundColor = [UIColor lightGrayColor];
rader.image = [UIImage imageNamed:@"IMG_1314.JPG"];
//点击按钮时两种状态调用的两种block
rader.block = ^{
NSLog(@"1");
};
rader.selectBlock = ^{
NSLog(@"2");
};
[self.view addSubview:rader];
}
@end
好了基本实现了。