美文网首页
IOS波浪效果的实现

IOS波浪效果的实现

作者: 云端飞扬_cb08 | 来源:发表于2017-09-29 14:29 被阅读0次

做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

然后就搞定了

相关文章

  • iOS实现波浪效果

    iOS实现波浪效果

  • iOS实现波浪效果

    这篇文章主要为大家详细介绍了iOS实现波浪效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分...

  • IOS波浪效果的实现

    做ios 好几年了也没写过什么,我开始写一下这个吧! 首先定义一个waves继承于UIView 的类Waves.h...

  • iOS波浪效果实现

    为了使界面看起来高大上,很多app都会加入各种各样的动态效果。本篇文章就简单谈谈波浪效果的实现,效果如下所示: 实...

  • iOS波浪效果超简单实现

    先上 GitHub地址 GLWaveView 介绍 一个自带波形动画的View, 可以添加多层波浪且方便自定义, ...

  • iOS波浪效果-OpenGL实现篇

    本文所用的代码在https://github.com/SquarePants1991/WaveEffect 工作需...

  • iOS- 波浪效果简单实现

    //只跟相位有关 (void)creat4{ CGFloatW =CGRectGetWidth(self.view...

  • iOS 波浪效果

    效果波浪效果 WaveView.h WaveView.m 实现

  • 波浪效果的实现

    1.创建layer _shapeLayer= [CAShapeLayer layer];_shapeLayer.f...

  • iOS 圆形水波浪效果实现

    水波浪效果如下 最近项目中用到一个小的波浪动画,查阅了一些资料如下:网上参考的一些效果CDNS上的原理 主要实现方...

网友评论

      本文标题:IOS波浪效果的实现

      本文链接:https://www.haomeiwen.com/subject/xdeiextx.html