CAShapeLayer实现音量大小动态改变
我是效果图
实现如图这效果一般会直接通过多张图进行切换进行完成。但这样的处理,会浪费App的资源存储空间,而且效率也不高。那么今天我们用CAShapeLayer实现以下吧。
拆分:
1.一个椭圆
2.一个矩形, 控制高度实现动画效果
3.一个圆弧
4.横线和竖线
添加图层和视图
CAShapeLayer *_shapeLayer2; // 矩形图层
UIView *_dynamicView; // 放置椭圆外框的视图
实现代码
- (void)voiceAnimation
{
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 145)];
backView.backgroundColor = [UIColor grayColor];
backView.center = self.view.center;
[self.view addSubview:backView]; // 外部轮廓View
_dynamicView = [[UIView alloc] initWithFrame:CGRectMake(85, 30, 30, 60)];
_dynamicView.layer.cornerRadius = 15;
_dynamicView.layer.masksToBounds = YES;
_dynamicView.clipsToBounds = YES;
[backView addSubview:_dynamicView]; // 添加椭圆
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_dynamicView.bounds cornerRadius:15];
shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 3.0f;
[_dynamicView.layer addSublayer:shapeLayer]; // 添加矩形
CGFloat height = 30;
_shapeLayer2 = [CAShapeLayer layer];
UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake(0, 60 - height, 30, height)];
_shapeLayer2.path = path2.CGPath;
_shapeLayer2.fillColor = [UIColor whiteColor].CGColor;
[_dynamicView.layer addSublayer:_shapeLayer2]; // 添加圆弧
CAShapeLayer *shapeLayer3 = [CAShapeLayer layer];
shapeLayer3.frame = backView.bounds;
UIBezierPath *path3 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(70, 35, 60, 65)];
shapeLayer3.path = path3.CGPath;
shapeLayer3.strokeStart = 0.00f;
shapeLayer3.strokeEnd = 0.50f;
shapeLayer3.fillColor = [UIColor clearColor].CGColor;
shapeLayer3.lineWidth = 5.0f;
shapeLayer3.strokeColor = [UIColor whiteColor].CGColor;
[backView.layer addSublayer:shapeLayer3]; // 添加竖线
CAShapeLayer *shapeLayer4 = [CAShapeLayer layer];
shapeLayer4.frame = backView.bounds;
UIBezierPath *path4 = [UIBezierPath bezierPath];
[path4 moveToPoint:CGPointMake(100, 100)];
[path4 addLineToPoint:CGPointMake(100, 115)];
shapeLayer4.path = path4.CGPath;
shapeLayer4.lineWidth = 5.0f;
shapeLayer4.strokeColor = [UIColor whiteColor].CGColor;
[backView.layer addSublayer:shapeLayer4]; // 画横线
CAShapeLayer *shapeLayer5 = [CAShapeLayer layer];
shapeLayer5.frame = backView.bounds;
UIBezierPath *path5 = [UIBezierPath bezierPath];
[path5 moveToPoint:CGPointMake(85, 115)];
[path5 addLineToPoint:CGPointMake(115, 115)];
shapeLayer5.path = path5.CGPath;
shapeLayer5.lineWidth = 5.0f;
shapeLayer5.strokeColor = [UIColor whiteColor].CGColor;
[backView.layer addSublayer:shapeLayer5];
}
改变大小的代码
- (void)refreshUIWithVoicePower:(CGFloat)voicePower
{
NSLog(@"%f", voicePower);
[_shapeLayer2 removeFromSuperlayer];
_shapeLayer2 = nil;
// 添加矩形
CGFloat height = 60 * voicePower;
_shapeLayer2 = [CAShapeLayer layer];
UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake(0, 60 - height, 30, height)];
_shapeLayer2.path = path2.CGPath;
_shapeLayer2.fillColor = [UIColor whiteColor].CGColor;
[_dynamicView.layer addSublayer:_shapeLayer2];
}
CAShapeLayer实现音量大小动态改变的更多相关文章
- iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现
最近同样是新App,设计稿里出现一种圆形进度条的设计,如下: 想了想,圆形进度条实现起来不难,但是其中显示百分比的文本确需要自适应,虽然可以使用时自己设定文本字体的大小,但是这样显得很麻烦,也很low ...
- 模仿iframe框架,由分隔栏动态改变左右两侧div大小———基于jQuery
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- 分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小
原文:分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小 /** * Reallocates an array with a new size, and copies the co ...
- 转载 * jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小
由jQuery实现上下.左右动态改变左右.上下两个div的大小,需要自己引入jquery1.8.0.min.js包 可用于页面布局. //============================ind ...
- jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小
由jQuery实现上下.左右动态改变左右.上下两个div的大小,需要自己引入jquery1.8.0.min.js包 可用于页面布局. //============================ind ...
- 使用jQuery动态改变图片显示大小
当我们要显示后台传过来若干个尺寸不一的图片时,为了保证图片大小的一致性及比例的协调,需要动态改变图片显示尺寸.通过搜索,我们可以从网上找到实现此 功能的jQuery代码如下.这段代码可以使图片的大小保 ...
- android 动态改变控件位置和大小 .
动态改变控件位置的方法: setPadding()的方法更改布局位置. 如我要把Imageview下移200px: ImageView.setPadding( ImageVie ...
- 由一次动态改变font-size的大小引申的一系列困惑补录
以下结论如有错误,欢迎指正 在切入正题之前,先了解下window 和document这两个大对象 我们熟知 JavaScript的组成如下图所示: window对象和document对象分别属于哪个分 ...
- Android中动态改变控件的大小的一种方法
在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现 一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...
随机推荐
- 64_t5
texlive-mkpattern-svn15878.1.2-33.fc26.2.noarch..> 24-May-2017 15:54 38178 texlive-mkpic-bin-svn3 ...
- .htaccess教程:简介、访问控制、验证、目录浏览控制
一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...
- 蓝图-BluePrint
蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...
- 20165301实验二java面向对象程序设计
20165301实验二java面向对象程序设计 实验目的与要求(提交点一): 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST完成单 ...
- DB2:FETCH FIRST 1 ROWS ONLY
DB2:FETCH FIRST ROWS ONLY,在Oracle中使用where rownum=1代替 DB2: SELECT INSTORAGENO FROM ( SELECT max(AUDIT ...
- linux sed命令查询结果前后批量追加内容(html文件批量修改css,js等文件路径)
1.需求:linux使用shell命令查询结果前后批量追加内容 例如:我需要在当前目录下所有的css文件路径前追加域名 我想的是用sed替换去实现,鲍哥的思路是用for循环 1.1方法1:鲍哥的for ...
- codeforce 1A Theatre Square
A. Theatre Square Theatre Square in the capital city of Berland has a rectangular shape with the siz ...
- IEEEXtreme 10.0 - Goldbach's Second Conjecture
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Goldbach's Second Conjecture 题目来源 第10届IEEE极限编程大赛 https ...
- 深入理解计算机系统项目之 Shell Lab
博客中的文章均为meelo原创,请务必以链接形式注明本文地址 Shell Lab是CMU计算机系统入门课程的一个实验.在这个实验里你需要实现一个shell,shell是用户与计算机的交互界面.普通意义 ...
- 【DEV C++】 Error: ld returned 1 exit status
一般出现“ld returned 1 exit status”错误都是由于函数名称拼写错误造成的,或者在一个工程中不同的函数使用了同一个函数名,暂时还未遇到其他情况.