CAShapeLayer+CADisplayLink 波浪动画

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1e9421 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #822d0f }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c81b13 }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3c828b }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000 }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c42275 }
p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #6122ae }
p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa }
p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3e1e81 }
p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #539aa4 }
p.p12 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #294c50 }
span.s1 { }
span.s2 { font: 14.0px "PingFang SC" }
span.s3 { color: #0435ff }
span.s4 { color: #822d0f }
span.s5 { color: #c42275 }
span.s6 { color: #000000 }
span.s7 { color: #6122ae }
span.s8 { color: #703daa }
span.s9 { color: #3e1e81 }
span.s10 { color: #294c50 }
span.s11 { color: #539aa4 }
span.s12 { color: #78492a }

//

//  KCLWaveView.m

//  ButtonAnimation

//

//  Created by kcl on 2017/5/4.

//  Copyright © 2017年 KCL. All rights reserved.

//

#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

上一篇:Python之路第三天,基础(3)-set,函数,内置函数,文件,三元运算,lambda


下一篇:[HAOI 2008]糖果传递