用drawRect以及CAReplicatorLayer绘制动态水波纹

大大简化了写水波纹效果的难度,你可以根据示例自己组装水波纹效果,本设计是几个工具组合在一起完成的效果, DrawRectObject 以及 ReplicatorLineAnimationView 均可以独立完成更复杂的功能.

说明

1. 用sine计算正玄曲线

2. 用CAReplicatorLayer实现重复移动的效果

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 中的 DrawRectObject

//
// WaveView.h
// DrawRectObject
//
// Created by YouXianMing on 16/8/1.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "CustomDrawingView.h" typedef enum : NSUInteger { kStrokeWave = 1 << 2,
kFillWave = 1 << 3 , } EWaveViewType; @interface WaveView : CustomDrawingView /**
* Wave type, default is kFillWave.
*/
@property (nonatomic) EWaveViewType type; /**
* Sine phase, default is 0.
*/
@property (nonatomic) CGFloat phase; /**
* Wave crest height, Default is 10.
*/
@property (nonatomic) CGFloat waveCrest; /**
* Full wave count, default is 1.
*/
@property (nonatomic) NSInteger waveCount; /**
* The fill style.
*/
@property (nonatomic, strong) DrawingStyle *fillStyle; /**
* The stroke style.
*/
@property (nonatomic, strong) DrawingStyle *strokeStyle; @end
//
// WaveView.m
// DrawRectObject
//
// Created by YouXianMing on 16/8/1.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "WaveView.h" @implementation WaveView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.waveCrest = 10.f;
self.waveCount = 1;
self.phase = 0.f;
self.type = kFillWave; DrawingStyle *fillStyle = [DrawingStyle new];
fillStyle.fillColor = [DrawingColor colorWithUIColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
self.fillStyle = fillStyle; DrawingStyle *strokeStyle = [DrawingStyle new];
strokeStyle.strokeColor = [DrawingColor colorWithUIColor:[UIColor redColor]];
strokeStyle.lineWidth = 0.5f;
self.strokeStyle = strokeStyle;
} return self;
} - (void)drawRect:(CGRect)rect { NSParameterAssert(self.fillStyle);
NSParameterAssert(self.strokeStyle); [super drawRect:rect]; CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height; if (self.type & kFillWave) { [self.drawRectObject useDrawingStyle:_fillStyle drawFillBlock:^(DrawRectObject *drawRectObject) { for (CGFloat x = 0; x <= width; x++) { if (x == 0) { [drawRectObject moveToStartPoint:CGPointMake(x, _waveCrest * sin((2 * M_PI) * _waveCount / width * x + _phase) + height / 2.f)];
continue; } else { [drawRectObject addLineToPoint:CGPointMake(x, _waveCrest * sin((2 * M_PI) * _waveCount / width * x + _phase) + height / 2.f)];
}
} [drawRectObject addLineToPoint:CGPointMake(width, height)];
[drawRectObject addLineToPoint:CGPointMake(0, height)];
[drawRectObject addLineToPoint:CGPointMake(0, _waveCrest * sin((2 * M_PI) * _waveCount / width * 0 + _phase) + height / 2.f)];
}];
} if (self.type & kStrokeWave) { [self.drawRectObject useDrawingStyle:_strokeStyle drawStrokeBlock:^(DrawRectObject *drawRectObject) { for (CGFloat x = 0; x <= width; x++) { if (x == 0) { [drawRectObject moveToStartPoint:CGPointMake(x, _waveCrest * sin((2 * M_PI) * _waveCount / width * x + _phase) + height / 2.f)];
continue; } else { [drawRectObject addLineToPoint:CGPointMake(x, _waveCrest * sin((2 * M_PI) * _waveCount / width * x + _phase) + height / 2.f)];
}
}
}];
}
} @end
//
// ViewController.m
// DrawRectObject
//
// Created by YouXianMing on 16/7/30.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "WaveView.h"
#import "ReplicatorLineAnimationView.h"
#import "UIView+SetRect.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Wave 1
{
WaveView *waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
waveView.phase = 0.f;
waveView.waveCrest = 5.f;
waveView.waveCount = 1;
waveView.type = kStrokeWave | kFillWave; {
DrawingStyle *fillStyle = [DrawingStyle new];
fillStyle.fillColor = [DrawingColor colorWithUIColor:[[UIColor redColor] colorWithAlphaComponent:0.25f]];
waveView.fillStyle = fillStyle; DrawingStyle *strokeStyle = [DrawingStyle new];
strokeStyle.strokeColor = [DrawingColor colorWithUIColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
strokeStyle.lineWidth = 0.5f;
waveView.strokeStyle = strokeStyle;
} ReplicatorLineAnimationView *replicatorLineView = [[ReplicatorLineAnimationView alloc] initWithFrame:waveView.bounds];
replicatorLineView.direction = kReplicatorLeft;
replicatorLineView.speed = 0.1f;
replicatorLineView.contentView = waveView;
[replicatorLineView startAnimation];
[self.view addSubview:replicatorLineView];
} // Wave 2
{
WaveView *waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
waveView.phase = 0.f;
waveView.waveCrest = 10.f;
waveView.waveCount = 1;
waveView.type = kStrokeWave | kFillWave; {
DrawingStyle *fillStyle = [DrawingStyle new];
fillStyle.fillColor = [DrawingColor colorWithUIColor:[[UIColor redColor] colorWithAlphaComponent:0.5f]];
waveView.fillStyle = fillStyle; DrawingStyle *strokeStyle = [DrawingStyle new];
strokeStyle.strokeColor = [DrawingColor colorWithUIColor:[UIColor redColor]];
strokeStyle.lineWidth = 0.5f;
waveView.strokeStyle = strokeStyle;
} ReplicatorLineAnimationView *replicatorLineView = [[ReplicatorLineAnimationView alloc] initWithFrame:waveView.bounds];
replicatorLineView.direction = kReplicatorLeft;
replicatorLineView.speed = 0.3f;
replicatorLineView.contentView = waveView;
[replicatorLineView startAnimation];
[self.view addSubview:replicatorLineView];
} // Wave 3
{
WaveView *waveView = [[WaveView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
waveView.phase = 10.f;
waveView.waveCrest = 15.f;
waveView.waveCount = 1;
waveView.type = kFillWave; {
DrawingStyle *fillStyle = [DrawingStyle new];
fillStyle.fillColor = [DrawingColor colorWithUIColor:[UIColor redColor]];
waveView.fillStyle = fillStyle;
} ReplicatorLineAnimationView *replicatorLineView = [[ReplicatorLineAnimationView alloc] initWithFrame:waveView.bounds];
replicatorLineView.direction = kReplicatorLeft;
replicatorLineView.speed = 0.5f;
replicatorLineView.contentView = waveView;
[replicatorLineView startAnimation];
[self.view addSubview:replicatorLineView];
}
} @end

用drawRect以及CAReplicatorLayer绘制动态水波纹的更多相关文章

  1. 自定义view实现水波纹效果

    水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...

  2. Android 自定义view实现水波纹效果

    http://blog.csdn.net/tianjian4592/article/details/44222565 在实际的开发中,很多时候还会遇到相对比较复杂的需求,比如产品妹纸或UI妹纸在哪看了 ...

  3. 用path动画绘制水波纹

    用path动画绘制水波纹 效果 源码 // // ViewController.m // PathAnimation // // Created by YouXianMing on 15/7/3. / ...

  4. 手把手教你画一个 逼格满满圆形水波纹loadingview Android

    才没有完结呢o( ̄︶ ̄)n .大家好,这里是番外篇. 拜读了爱哥的博客,又学到不少东西.爱哥曾经说过: 要站在巨人的丁丁上. 那么今天,我们就站在爱哥的丁丁上来学习制作一款自定义view(开个玩笑,爱 ...

  5. Android -- 贝塞尔实现水波纹动画(划重点!!)

    1,昨天看到了一个挺好的ui效果,是使用贝塞尔曲线实现的,就和大家来分享分享,还有,在写博客的时候我经常会把自己在做某种效果时的一些问题给写出来,而不是像很多文章直接就给出了解决方法,这里给大家解释一 ...

  6. Android特效专辑(一)——水波纹过渡特效(首页)

    Android特效专辑(一)--水波纹过渡特效(首页) 也是今天看到的一个特效,感觉挺漂亮的,最近也一直在筹划一个APP,就想把他当做APP的首页,然后加些处理,关于首页APP的特效等我完工了再贴出来 ...

  7. 适配移动端的在图片上生成水波纹demo

      <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&q ...

  8. Android自定义控件-Path之贝赛尔曲线和手势轨迹、水波纹效果

    从这篇开始,我将延续androidGraphics系列文章把图片相关的知识给大家讲完,这一篇先稍微进阶一下,给大家把<android Graphics(二):路径及文字>略去的quadTo ...

  9. 三角函数之美-水波纹载入LoadingView

    一.前言 学习是要总结的.近期几天学习了画图相关的,可是使用的机会较少,如今又快要遗忘了,这次看了水波纹的绘制.认为十分有意思,还是 把实现的方法记录下来.技术无他,为手熟尔.还是要多练习,空淡误国, ...

随机推荐

  1. Zookeeper集群搭建以及python操作zk

    一.Zookeeper原理简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. Zookeeper设计目 ...

  2. vuex 的使用

    用于多组件共享状态,如果不打算开发大型单页应用,使用 Vuex 可能是繁琐冗余的.确实是如此——如果应用够简单,您最好不要使用 Vuex.可使用简单Bus总线的方式来管理共享的数据详见(http:// ...

  3. Java编程的逻辑 (1) - 数据和变量

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  4. Mysql中count(*)和limit同时使用的问题

    1.带有count的sql语句只会返回一条记录 , 结果如下图: 2.带有limit的sql语句是最后执行的 , 以上sql语句将返回空行,因为count(*)只会使sql语句产生一条结果记录,所以后 ...

  5. Codeforces 379F New Year Tree 树的直径的性质推理

    New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...

  6. 解决 Could not resolve com.android.tools.build:gradle:3.1.3

      android studio 升级到3.1.3后总会遇到莫名其妙的错误,前几天刚解决了项目 dependencies报错的问题. 解决android studio 升级到3.0+之后 项目 dep ...

  7. hdu 4169 二分匹配最大独立集 ***

    题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...

  8. 慢查询日志分析工具之mysqldumpslow

    使用方法: 在 windows 下可以在mysql/bin目录下可以找到 mysqldumpslow.pl 文件,是一个 perl 格式的文件,如下图: 这种格式的文件在windows下是不能直接执行 ...

  9. CentOS 7下宿主机使用virsh console访问KVM的设置

    在CentOS 6下要实现宿主机使用virsh console访问KVM可以说是非常麻烦,但这一问题在CentOS 7已经解决了,只需要两条命令在KVM下即可实现. 1.在KVM(客户机)下开机启动并 ...

  10. github入门教程:第一步

    [git教程] 以前在网上找过一些,见 http://www.wojilu.com/Forum1/Topic/702  我自己会一边学,一边写教程,过程中有不明白的,会跟大家请教交流.   ----- ...