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不行. ...
随机推荐
- Linux下搜索命令
linux下用于查找文件的5个命令,有需要的朋友可以参考下.包括find,whereis,locate,which与type. linux下用于查找文件的5个命令,有需要的朋友可以参考下.包括find ...
- 29、最小的K个数
一.题目 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.解法 import java.util.ArrayList; ...
- 1-编程基础及Python环境部署
目录 1 编程基础 1.1 基本概念 1.2 语言分类 1.3 高级语言的发展 2 程序 3 python的语言介绍 4 Python的解释器 5 Python版本区别 6 Python安装 6.1 ...
- MGR_ERROR 3092 (HY000): DROP DATABASE failed;
start group_replication;时报以下错: ERROR 3092 (HY000): DROP DATABASE failed; some tables may have been d ...
- C/C++——static修饰符
1. static变量 static 用来说明静态变量.如果是在函数外面定义的,那么其效果和全局变量类似,但是,static定义的变量只能在当前c程序文件中使用,在另一个c代码里面,即使使用exter ...
- sort排序命令常见用法
sort -n 按数字排序 [root@test88 ~]# cat test.txt 19036 6111 24039 3660 20610 10937 32408 20744 8248 28255 ...
- laravel入门教程
参考地址:https://github.com/johnlui/Learn-Laravel-5/issues/16
- redis局域网内开启访问
若需要开启A(192.168.0.3)的访问1.配置confg bind 192.168.0.3 2.设置访问密码 requirepass password 3.重新载入配置 ./redis-serv ...
- 设置或者获取CheckboxList控件的选中值
1.设置CheckBoxList选中的值 /// <summary> /// 设置CheckBoxList中哪些是选中了的 /// </summary> /// <par ...
- SGU 209. Areas
209. Areas time limit per test: 0.25 sec.memory limit per test: 65536 KB input: standardoutput: stan ...