iOS 不同颜色之分段式圆环,段数不固定
来源:互联网 发布:nginx需要密码访问 编辑:程序博客网 时间:2024/05/22 18:35
最近美工那边提出一个需求,就是需要展示资产的占比,通过一个圆环展示,于是自己便封装了一个。段数不限制,只需赋值所占比例及该段的颜色即可。
float a=301.25,b=235.23,c=452.65;
ColorCircleView *view = [[ColorCircleViewalloc]initWithFrame:CGRectMake(100,200,100, 100)];
view.circleArray =@[
@{
@"strokeColor":[UIColorredColor],
@"precent" :@(a/(a+b+c))
},
@{
@"strokeColor":[UIColororangeColor],
@"precent" :@(b/(a+b+c))
},
@{
@"strokeColor":[UIColoryellowColor],
@"precent" :@(c/(a+b+c))
}
];
[self.viewaddSubview:view];
运行效果如下:
#import <UIKit/UIKit.h>
@interface ColorCircleView : UIView
//数组里面装的是字典,,字典里有两个key -> strokeColor和precent
@property (nonatomic,assign)NSArray *circleArray;
@end
#import "ColorCircleView.h"
@interface ColorCircleView ()
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@end
@implementation ColorCircleView
- (void)initType
{
__blockfloat a =0;
[self.circleArrayenumerateObjectsUsingBlock:^(NSDictionary *obj,NSUInteger idx, BOOL *_Nonnull stop) {
//创建出CAShapeLayer
self.shapeLayer = [CAShapeLayerlayer];
self.shapeLayer.frame =CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height);//设置shapeLayer的尺寸和位置
// self.shapeLayer.position = self.view.center;
self.shapeLayer.fillColor = [UIColorclearColor].CGColor;//填充颜色为ClearColor
//设置线条的宽度和颜色
self.shapeLayer.lineWidth =10.0f;
self.shapeLayer.strokeColor = [obj[@"strokeColor"]CGColor];
//创建出圆形贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,self.bounds.size.width,self.bounds.size.height)];
//让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayer.path = circlePath.CGPath;
self.shapeLayer.strokeStart = a;
self.shapeLayer.strokeEnd = [obj[@"precent"]floatValue] + a;
a = self.shapeLayer.strokeEnd;
//添加并显示
[self.layeraddSublayer:self.shapeLayer];
//添加圆环动画
CABasicAnimation *pathAnimation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];
pathAnimation.duration =1.0;
pathAnimation.fromValue =@(0);
pathAnimation.toValue =@(1);
pathAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];
[self.shapeLayeraddAnimation:pathAnimationforKey:@"strokeEnd"];
}];
}
- (void)setCircleArray:(NSArray *)circleArray
{
_circleArray = circleArray;
[selfinitType];
}
@end
- iOS 不同颜色之分段式圆环,段数不固定
- 操作系统之分段式内存管理1
- 操作系统存储管理之分段式与段页式虚拟存储系统
- 分段式内存管理
- 分段式RadioGroup
- 分段式进度条
- iOS开发之自定义圆环式Slider
- iOS实用技能之同一个String不同颜色的字
- 闲聊SEDA分段式事件驱动架构
- Unity UGUI实现分段式血条
- 操作系统——分段式内存管理
- 分段式显示Boss血条思路
- ios 画圆环进度条
- iOS 颜色渐变圆环
- ios 圆环加载动画
- ios 画圆环进度条
- Unity 之圆环算法
- #学习笔记#(11)四分圆环-photoshop
- 每天laravel-20160620|MemcachedConnector
- 码农小汪-剑指Offer之12 -链表中倒数第k个结点
- python学习的一些小技巧小结
- C# 全局变量与局部变量
- mysql Source导入问题
- iOS 不同颜色之分段式圆环,段数不固定
- 通过AndroidTestCase来进行android 单元测试 part II
- android5.1+获取当前运行的app2
- 数据探索之缺失值处理及代码实现
- 升级win10后virtualBox无法使用
- 如何调用自己写的python函数?
- 泛读:CVPR2014:Discriminative Deep Metric Learning for Face Verification in theWild
- Xcode7--免证书真机调试
- vim 空格问题