做ios 好几年了也没写过什么,我开始写一下这个吧!
首先定义一个waves继承于UIView 的类Waves.h Waves.m
一:
Waves.h 如下:
#import@interface Waves : UIView
@property (nonatomic,assign)CGFloat waveA;//水纹振幅
@property (nonatomic,assign)CGFloat waveW ;//水纹周期
@property (nonatomic,assign)CGFloat offsetX; //位移
@property (nonatomic,assign)CGFloat currentK;//当前波浪高度Y
@property (nonatomic,assign)CGFloat wavesSpeed;//水纹速度
@property (nonatomic,assign)CGFloat WavesWidth; //水纹宽度
@property (nonatomic,assign)BOOL iscos; //水纹方式
@property (nonatomic,strong)UIColor *waveColor;//颜色
-(void)waveStart;
@end
Waves.m 如下:
#import "Waves.h"
@interface Waves()
@property (nonatomic,strong)CADisplayLink *wavesDisplayLink;
@property (nonatomic,strong)CAShapeLayer *wavesLayer;
@end
@implementation Waves
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.layer.masksToBounds = YES;
_WavesWidth = self.frame.size.width;
_wavesSpeed = 1/M_PI;
_currentK = self.frame.size.height/2;//屏幕居中
// _color = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1];
}
return self;
}
- (void)waveStart{
//初始化layer
if (self.wavesLayer == nil) {
//初始化
self.wavesLayer = [CAShapeLayer layer];
//设置闭环的颜色
self.wavesLayer.fillColor = self.waveColor.CGColor;
[self.layer addSublayer:self.wavesLayer];
}
//设置波浪纵向位置
//启动定时器
self.wavesDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];
[self.wavesDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)getCurrentWave:(CADisplayLink *)displayLink{
_offsetX += _wavesSpeed;
[self setCurrentFirstWaveLayerPath];
}
-(void)setCurrentFirstWaveLayerPath{
//创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
CGFloat y = _currentK;
//将点移动到 x=0,y=currentK的位置
CGPathMoveToPoint(path, nil, 0, y);
for (NSInteger i =0.0f; i<=_WavesWidth; i++) {
//余弦函数波浪公式
if (_iscos) {
y = _waveA * cos(_waveW*i + _offsetX)+_currentK;
}else{
y = _waveA * sin(_waveW * i+ _offsetX)+_currentK;
}
//将点连成线
CGPathAddLineToPoint(path, nil, i, y);
}
CGPathAddLineToPoint(path, nil, _WavesWidth, 0);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);
self.wavesLayer.path = path;
CGPathRelease(path);
}
-(void)dealloc
{
[self.wavesDisplayLink invalidate];
}
二:在控制器ViewController 引入
#import "ViewController.h"
#import "Waves.h"
@interface ViewController ()
@property (nonatomic,strong)Waves *firstWare;
@property (nonatomic,strong)Waves *secondWare;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.firstWare = [[Waves alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 220)];
self.firstWare.waveColor = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1];
self.firstWare.alpha=0.6;
self.firstWare.wavesSpeed = 0.02;
//设置振幅
self.firstWare.waveA = 12;
//设置周期
self.firstWare.waveW = 0.5/30.0;
//第二个波浪
self.secondWare = [[Waves alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 220)];
self.secondWare.waveColor = [UIColor colorWithRed:86/255.0f green:202/255.0f blue:139/255.0f alpha:1];
self.secondWare.alpha=0.6;
self.secondWare.iscos = YES;
self.secondWare.wavesSpeed = 0.04;
//设置振幅
self.secondWare.waveA = 13;
//设置周期
self.secondWare.waveW = 0.5/30.0;
[self.view addSubview:self.firstWare];
[self.view addSubview:self.secondWare];
[self.secondWare waveStart];
[self.firstWare waveStart];
//是否有震荡效果
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(animateWave) userInfo:nil repeats:YES];
}
}
@end
然后就搞定了