动态实时设置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. tomcat启动(六)Catalina分析-StandardServer.start()

    从链接 Tomcat中组件的生命周期管理公共接口Lifecycle 可以知道调用的是StandardServer.startInternal() @Override protected void st ...

  2. asterisk与freepbx常用的命令

    asterisk 常用命令: 通过asterisk -r 连接我们的asterisk. 在CLI中常用的命令: sip show peers 显示所有的SIP peers(包括friends) sip ...

  3. [笔记] print函数进阶

      1.1print(values=None,sep='',end='\n',file=sys.stdout,flush=False) 参数: values:输出到控制台的string sep:设置输 ...

  4. after_create and after_commit

    A relational database, like mysql, provides transactions to wrap several operations in one unit, mak ...

  5. Rails中activeAdmin的使用

    一.开始ActiveAdmin Active Admin是一个发布在RAILS3中使用的Gem. 1.我们为了快速开始我们对Active Admin的了解,我们首先安装它: 在你GemFile中添加g ...

  6. Window环境配置Mongodb

    Mongodb这几天也了解了一下,今天配置了下环境,从今天开始学下Mongodb数据库. 一.下载 在这个网址中选择要下载的开发环境https://www.mongodb.com/download-c ...

  7. 15.Generator 函数的语法

    Generator 函数的语法 Generator 函数的语法 简介 基本概念 Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同.本章详细介绍 Generat ...

  8. c#基础学习(0703)之string.Format格式化日期

    C# string.Format格式化日期 DateTime dt = ,,,,,,); string.Format("{0:y yy yyy yyyy}",dt); //17 1 ...

  9. c#基础学习(0628)之使用进程打开指定的文件、模拟磁盘打开文件

    使用进程打开指定的文件 模拟磁盘打开文件 class Program { static void Main(string[] args) { while(true) { Console.WriteLi ...

  10. C#反射调用外部Dll,执行其中异步函数并取返回值

    using System.Reflection; 1.载入Dll Assembly asm=Assembly.LoadFile(FullPath);//FullPath 为Dll所在位置的全路径. 2 ...