动态实时设置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贝塞尔曲线的坐标点的更多相关文章

  1. 为控件Button设置快捷键(组合键)

    控件MenuStrip和ContextMenuStrip可通过ShortCcutKeys属性设置快捷键,而控件Button没有ShortcutKey属性,如何为控件Button设置快捷键呢(组合件键) ...

  2. Delphi HTTPRIO控件怎么设置超时参数

    HTTPRIO控件怎么设置超时参数 //HTTPRIO1: THTTPRIO  设置5分钟超时 HTTPRIO1.HTTPWebNode.ConnectTimeout := 5000; Connect ...

  3. Android中EditText,Button等控件的设置

    EditText可以使用:TextView.setEnabled(true)来设置为可编辑,其实很简单,写在这里以便以后自己查看. Button设置可用性:setVisibility(View.VIS ...

  4. ScrollView子控件高度设置无效

    ScrollView子控件高度设置无效 简述 项目中引入了第三方的下拉刷新包PullToRefreshScrollView. 由于我之前布局未考虑下拉刷新功能.后来暂时发现添加上去,发现.子控件的高度 ...

  5. dev控件ASPxComboBox设置ReadOnly="true"后

    dev控件ASPxComboBox设置ReadOnly="true"后,在后台OnCallback事件中赋值前台不显示

  6. DEV控件ASPxTextBox设置ClientEnabled="false"之后出现的问题

    DEV控件ASPxTextBox设置ClientEnabled="false"之后,js中设置文本框的值后,按钮后台点击事件中获取文本框的值为空.

  7. duilib 修复Text控件无法设置宽度的bug,增加自动加算宽度的属性

    转载请说明原出处,谢谢~~: 今天有朋友反映CTextUI控件无法设置宽度,于是修复了这个bug,顺便给Text控件增加了一个自动计算宽度的属性,描述如下 <Attribute name=&qu ...

  8. 几个关于控件的优先级: UseSystemPasswordChar > PasswordChar > 控件属性设置

    using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...

  9. DevExpress 控件中设置分隔符

    原文:DevExpress 控件中设置分隔符 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...

随机推荐

  1. java 实现 HTTP请求(GET、POST)的方法

    使用Java进行服务调用时,避免不了要使用模拟HTTP请求来实现模拟,我在开发过程中恰巧遇到了这类的业务需求,所以就对这类的方法进行了一次总结,原理层次的东西暂时不考虑,毕竟HTTP的底层实现啥的,东 ...

  2. pycharm的python console报错CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 87, in init self.matchers.remove(self.python_matches) ValueError: list.remove(x): x not in list

    卸载ipython pip uninstall ipython 安装ipython6.2.0 pip install ipython==6.2.0

  3. springcloud-07-eureka HA的高可用配置

    单机版的eureka, 运行时间稍长, 就会在管理界面出现红色的警告, 为了消除这个警告, 可以使用eureka的高可用配置: 只需要写一个工程配置不同的配置文件, 然后启动多实例即可: 请参照单机版 ...

  4. libnetwork插件化网络功能

    Docker把网络跟存储这两部分的功能实现都以插件化形式剥离出来,允许用户通过指令来选择不同的后端实现.这也是Docker希望构建围绕着容器的强大生态系统的一些积极的尝试.剥离出来的独立容器网络项目叫 ...

  5. 安装sublime text2 for ubuntu

        Add our Sublime Text 2 Ubuntu PPA using the following commands: sudo add-apt-repository ppa:webu ...

  6. Struts2 Web Project 实现中文、英语的切换

    1.struts.xml文件部分配置: <package name="default" namespace="/login" extends=" ...

  7. C#语法之Linq查询基础一

    Linq做.Net开发的应该都用过,有些地方很复杂的逻辑用Linq很方便的解决.对于Linq to object.Linq to xml.Linq to sql.Linq to Entity(EF)都 ...

  8. .NET编译过程

    总结一下.NET的编译过程, 一般的高级编程语言会把代码编译成机器码,也就是我们说的非托管代码,执行在编译它的电脑上. 而.NET编译代码的时候会把高级编程语言编译成中间语言 运行在CLR(公共语言运 ...

  9. 对工厂方法模式的一些思考(java语法表示)

    同为创造型设计模式的简单工厂模式可以理解为对new关键字的代替. 本着重复三次即重构的原则,如果一个对象在不同的地方被new了两次以上,那就可以考虑使用它.那我们为什么要用简单工厂模式代替new呢?就 ...

  10. Java8简明学习之Lambda表达式

    函数式接口 就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口,函数式接口可以被隐式转换为lambda表达式. 之前已有的函数式接口: java.lang.Runnable java.uti ...