iOS|iOS CAShapeLayer+CADisplayLink 波浪动画
文章图片
042ec9e15de70b28500fa5b9e06883be.jpg 【iOS|iOS CAShapeLayer+CADisplayLink 波浪动画】代码就懒得写注释了、自己去研究吧。。。
#define BackGroundColor [UIColor colorWithRed:96/255.0f green:159/255.0f blue:150/255.0f alpha:1]
#define WaveColor1 [UIColor colorWithRed:136/255.0f green:199/255.0f blue:190/255.0f alpha:1]
#define WaveColor2 [UIColor colorWithRed:28/255.0 green:203/255.0 blue:174/255.0 alpha:1]#import "KCLWaveView.h"@interface KCLWaveView(){CAShapeLayer *_waveLayer1;
CAShapeLayer *_waveLayer2;
CADisplayLink *_disPlayLink;
CGFloat _waveAmplitude;
CGFloat _wavePalstance;
CGFloat _waveX;
CGFloat _waveY;
CGFloat _waveMoveSpeed;
}//@property (nonatomic,strong)
@end
@implementation KCLWaveView- (instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];
if (self) {
[self setUI];
[self buildData];
}
return self;
}
- (void)setUI{_waveLayer1 = [CAShapeLayer layer];
_waveLayer1.fillColor = WaveColor1.CGColor;
_waveLayer1.strokeColor = WaveColor1.CGColor;
[self.layer addSublayer:_waveLayer1];
_waveLayer2 = [CAShapeLayer layer];
_waveLayer2.fillColor = WaveColor2.CGColor;
_waveLayer2.strokeColor = WaveColor2.CGColor;
[self.layer addSublayer:_waveLayer2];
self.layer.cornerRadius = self.bounds.size.width/2.0f;
self.layer.masksToBounds = YES;
self.backgroundColor = BackGroundColor;
}- (void)buildData{_waveAmplitude = 10;
_wavePalstance = M_PI/self.bounds.size.width;
_waveX = 0;
_waveY = 0;
_waveMoveSpeed = _wavePalstance *2;
_disPlayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(upDataWave:)];
[_disPlayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)upDataWave:(CADisplayLink *)link{_waveX += _waveMoveSpeed;
[self updataWaveY];
[self updataWave1];
[self updataWave2];
}- (void)updataWaveY{CGFloat targetY = self.bounds.size.height - 0.3 * self.bounds.size.height;
if (_waveY < targetY) {
_waveY += 2;
}
if (_waveY > targetY ) {
_waveY -= 2;
}
}- (void)updataWave1{CGFloat waterWaveWidth = self.bounds.size.width;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil,0, _waveY);
CGFloat y = _waveY;
for (float x = 0.0f;
x<= waterWaveWidth;
x++) {y = _waveAmplitude *cos(_wavePalstance*x +_waveX)+_waveY;
CGPathAddLineToPoint(path, nil, x, y);
}
CGPathAddLineToPoint(path, nil, waterWaveWidth, self.bounds.size.height);
CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
CGPathCloseSubpath(path);
_waveLayer1.path = path;
CGPathRelease(path);
}- (void)updataWave2{CGFloat waterWaveWidth = self.bounds.size.width;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil,0, _waveY);
CGFloat y = _waveY;
for (float x = 0.0f;
x<= waterWaveWidth;
x++) {
y = _waveAmplitude *sin(_wavePalstance*x +_waveX)+_waveY;
CGPathAddLineToPoint(path, nil, x, y);
}CGPathAddLineToPoint(path, nil, waterWaveWidth, self.bounds.size.height);
CGPathAddLineToPoint(path, nil, 0, self.bounds.size.height);
CGPathCloseSubpath(path);
_waveLayer2.path = path;
CGPathRelease(path);
}- (void)dealloc{[self stop];
if (_waveLayer1) {
[_waveLayer1 removeFromSuperlayer];
_waveLayer1 = nil;
}if (_waveLayer2) {
[_waveLayer2 removeFromSuperlayer];
_waveLayer2 = nil;
}
}- (void)stop{
if (_disPlayLink) {
[_disPlayLink invalidate];
_disPlayLink = nil;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/@end
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- iOS,打Framework静态库
- 常用git命令总结