[控件] 动态实时设置CAShapeLayer贝塞尔曲线的坐标点
动态实时设置CAShapeLayer贝塞尔曲线的坐标点

效果图:

源码:
PathDirectionView.h 与 PathDirectionView.m
//
// PathDirectionView.h
// Path
//
// Created by XianMingYou on 15/2/27.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h>
#import "UIView+SetRect.h" @interface PathDirectionView : UIView /**
* 起始点在右边
*/
@property (nonatomic) BOOL startPointAtRight; /**
* 根据百分比显示
*
* @param percent 百分比
*/
- (void)showPercent:(CGFloat)percent; @end
//
// PathDirectionView.m
// Path
//
// Created by XianMingYou on 15/2/27.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "PathDirectionView.h" @interface PathDirectionView () {
CAShapeLayer *_shapeLayer;
} @end @implementation PathDirectionView /**
* 修改当前view的backupLayer为CAGradientLayer
*
* @return CAGradientLayer类名字
*/
+ (Class)layerClass {
return [CAShapeLayer class];
} - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_shapeLayer = (CAShapeLayer *)self.layer;
_shapeLayer.fillColor = [[UIColor clearColor] CGColor];
_shapeLayer.strokeColor = [[UIColor redColor] CGColor];
_shapeLayer.lineWidth = .f;
_shapeLayer.strokeEnd = .f;
_shapeLayer.opacity = .f;
_shapeLayer.path = [self createPathWithHeight:];
}
return self;
} /**
* 创建出贝塞尔曲线
*
* @param height 高度
*
* @return 贝塞尔曲线
*/
- (CGPathRef)createPathWithHeight:(CGFloat)height {
UIBezierPath *bezierPath = UIBezierPath.bezierPath; CGPoint startPoint = CGPointZero;
CGPoint endPoint = CGPointZero;
if (self.startPointAtRight == NO) {
startPoint = CGPointMake(self.width, height);
endPoint = CGPointZero;
} else {
startPoint = CGPointMake(, height);
endPoint = CGPointMake(self.width, );
} [bezierPath moveToPoint:startPoint];
[bezierPath addLineToPoint:endPoint]; return bezierPath.CGPath;
} - (void)showPercent:(CGFloat)percent { if (percent < ) {
_shapeLayer.path = [self createPathWithHeight:];
_shapeLayer.strokeEnd = ;
_shapeLayer.opacity = ;
} else if (percent >= && percent <= 0.5f) { // [0, 0.5]
_shapeLayer.path = [self createPathWithHeight:];
_shapeLayer.strokeEnd = percent * .f;
_shapeLayer.opacity = percent * .f;
} else if (percent <= .f) { // (0.5, 1]
CGFloat currentPercent = percent - 0.5f;
_shapeLayer.path = [self createPathWithHeight:currentPercent * self.height * ];
_shapeLayer.strokeEnd = .f;
_shapeLayer.opacity = .f;
} else { // (1, +无穷大)
_shapeLayer.path = [self createPathWithHeight:self.height];
_shapeLayer.strokeEnd = .f;
_shapeLayer.opacity = .f;
}
} @end
ShowDownView.h 与 ShowDownView.m
//
// ShowDownView.h
// Path
//
// Created by XianMingYou on 15/2/27.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h>
#import "PathDirectionView.h" @interface ShowDownView : UIView - (void)showPercent:(CGFloat)percent; @end
//
// ShowDownView.m
// Path
//
// Created by XianMingYou on 15/2/27.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "ShowDownView.h" @interface ShowDownView () @property (nonatomic, strong) PathDirectionView *leftView;
@property (nonatomic, strong) PathDirectionView *rightView; @end @implementation ShowDownView - (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
CGFloat width = frame.size.width / .f;
CGFloat height = frame.size.height; CGRect leftRect = CGRectMake(, , width, height);
CGRect rightRect = CGRectMake(width, , width, height); self.leftView = [[PathDirectionView alloc] initWithFrame:leftRect];
self.rightView = [[PathDirectionView alloc] initWithFrame:rightRect];
self.rightView.startPointAtRight = YES;
[self addSubview:self.leftView];
[self addSubview:self.rightView];
}
return self;
} - (void)showPercent:(CGFloat)percent {
[self.leftView showPercent:percent];
[self.rightView showPercent:percent];
} @end
核心原理:
1. 即时的根据值的变化重新生成path值并赋值给CAShapeLayer

2. 即时的根据值得变化设定strokeEnd值

[控件] 动态实时设置CAShapeLayer贝塞尔曲线的坐标点的更多相关文章
- 为控件Button设置快捷键(组合键)
控件MenuStrip和ContextMenuStrip可通过ShortCcutKeys属性设置快捷键,而控件Button没有ShortcutKey属性,如何为控件Button设置快捷键呢(组合件键) ...
- Delphi HTTPRIO控件怎么设置超时参数
HTTPRIO控件怎么设置超时参数 //HTTPRIO1: THTTPRIO 设置5分钟超时 HTTPRIO1.HTTPWebNode.ConnectTimeout := 5000; Connect ...
- Android中EditText,Button等控件的设置
EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...
- ScrollView子控件高度设置无效
ScrollView子控件高度设置无效 简述 项目中引入了第三方的下拉刷新包PullToRefreshScrollView. 由于我之前布局未考虑下拉刷新功能.后来暂时发现添加上去,发现.子控件的高度 ...
- dev控件ASPxComboBox设置ReadOnly="true"后
dev控件ASPxComboBox设置ReadOnly="true"后,在后台OnCallback事件中赋值前台不显示
- DEV控件ASPxTextBox设置ClientEnabled="false"之后出现的问题
DEV控件ASPxTextBox设置ClientEnabled="false"之后,js中设置文本框的值后,按钮后台点击事件中获取文本框的值为空.
- duilib 修复Text控件无法设置宽度的bug,增加自动加算宽度的属性
转载请说明原出处,谢谢~~: 今天有朋友反映CTextUI控件无法设置宽度,于是修复了这个bug,顺便给Text控件增加了一个自动计算宽度的属性,描述如下 <Attribute name=&qu ...
- 几个关于控件的优先级: UseSystemPasswordChar > PasswordChar > 控件属性设置
using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...
- DevExpress 控件中设置分隔符
原文:DevExpress 控件中设置分隔符 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...
随机推荐
- Maven可以提交到官方公共仓库maven.org
参考http://central.sonatype.org/pages/ossrh-guide.html这个网址的操作,即可提交到maven.org. 这里有具体的实践方法:http://blog.c ...
- C# byte 和 char 转化
C# byte 和 char 可以认为是等价的.但是在文本显示的时候有差异. c# 使用的是unicode字符集,应该和为ascii相互转换 只能转换到字符的unicode编码,或者由unico ...
- 简单的node爬虫练手,循环中的异步转同步
简单的node爬虫练手,循环中的异步转同步 转载:https://blog.csdn.net/qq_24504525/article/details/77856989 看到网上一些基于node做的爬虫 ...
- js如何判断IE浏览器的版本包括IE11
IE浏览器真是个坑:从ie6以及以前IE版本,简直就是垃圾,不按照Mozilla国际组织的标准来,乱搞.搞得兼容性很差: <script type="text/javascript ...
- Servlet 网页重定向
当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向.当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向. 重定向请求到另一个网页的最简单的方 ...
- spring boot 报错 Failed to read HTTP message
2008-12-13 15:06:03,930 WARN (DefaultHandlerExceptionResolver.java:384)- Failed to read HTTP message ...
- BIO、NIO和AIO的区别
一:事件分离器 在IO读写时,把 IO请求 与 读写操作 分离调配进行,需要用到事件分离器.根据处理机制的不同,事件分离器又分为:同步的Reactor和异步的Proactor. Reactor模型: ...
- gRPC的通讯过程
在 HTTP2 协议正式开始工作前, 如果已经知道服务器是 HTTP2 的服务器, 通讯流程如下: 客户端必须首先发送一个连接序言,其逻辑结构: PRI * HTTP/2.0\r\n\r\nSM\r\ ...
- HTTP协议之内容协商
一个URL常常需要代表若干不同的资源.例如那种需要以多种语言提供其内容的网站站点.如果某个站点有说法语的和说英语的两种用户,它可能想用这两种语言提供网站站点信息.理想情况下,服务器应当向英语用户发送英 ...
- 中小型研发团队架构实践五:Redis快速入门及应用
Redis的使用难吗?不难,Redis用好容易吗?不容易.Redis的使用虽然不难,但与业务结合的应用场景特别多.特别紧,用好并不容易.我们希望通过一篇文章及Demo,即可轻松.快速入门并学会应用. ...