[控件] 将字符串转换成贝塞尔曲线并执行动画
将字符串转换成贝塞尔曲线并执行动画
部分开源代码支持:
https://github.com/aderussell/string-to-CGPathRef
效果:
源码:
//
// ShapeWordView.h
// PathWord
//
// Created by XianMingYou on 15/3/6.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
//#import <UIKit/UIKit.h>
#import "UIBezierPath+TextPaths.h"@interface ShapeWordView : UIView@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, strong) UIColor *lineColor;
@property (nonatomic, assign) CGFloat lineWidth;/*** 创建view*/
- (void)buildView;/*** 百分比** @param percent 百分比*/
- (void)percent:(CGFloat)percent animated:(BOOL)animated;@end
//
// ShapeWordView.m
// PathWord
//
// Created by XianMingYou on 15/3/6.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
//#import "ShapeWordView.h"@interface ShapeWordView ()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end@implementation ShapeWordView- (void)buildView {// 过滤数据CGFloat lineWidth = (self.lineWidth <= 0 ? 0.5 : self.lineWidth);UIFont *font = (self.font == nil ? [UIFont systemFontOfSize:18.f] : self.font);UIColor *lineColor = (self.lineColor == nil ? [UIColor blackColor] : self.lineColor);NSString *text = self.text;if (text == nil || text.length == 0) {return;}// 初始化layerself.shapeLayer = [CAShapeLayer layer];self.shapeLayer.frame = self.bounds;self.shapeLayer.lineWidth = lineWidth;self.shapeLayer.fillColor = [UIColor clearColor].CGColor;self.shapeLayer.strokeColor = lineColor.CGColor;self.shapeLayer.path = [UIBezierPath pathForMultilineString:textwithFont:fontmaxWidth:self.bounds.size.widthtextAlignment:NSTextAlignmentCenter].CGPath;self.shapeLayer.bounds = CGPathGetBoundingBox(self.shapeLayer.path);self.shapeLayer.geometryFlipped = YES;self.shapeLayer.strokeEnd = 0.f;[self.layer addSublayer:self.shapeLayer];
}- (void)percent:(CGFloat)percent animated:(BOOL)animated {if (animated) {if (percent <= 0) {self.shapeLayer.strokeEnd = 0;} else if (percent > 0 && percent <= 1) {self.shapeLayer.strokeEnd = percent;} else {self.shapeLayer.strokeEnd = 1.f;}} else {if (percent <= 0) {[CATransaction setDisableActions:YES];self.shapeLayer.strokeEnd = 0;[CATransaction setDisableActions:NO];} else if (percent > 0 && percent <= 1) {[CATransaction setDisableActions:YES];self.shapeLayer.strokeEnd = percent;[CATransaction setDisableActions:NO];} else {[CATransaction setDisableActions:YES];self.shapeLayer.strokeEnd = 1.f;[CATransaction setDisableActions:NO];}}
}@end
使用:
//
// ViewController.m
// PathWord
//
// Created by XianMingYou on 15/3/6.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
//#import "ViewController.h"
#import "UIBezierPath+TextPaths.h"
#import "ShapeWordView.h"@interface ViewController ()<UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ShapeWordView *shape;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.tableView = [[UITableView alloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];self.tableView.delegate = self;[self.view addSubview:self.tableView];self.shape = [[ShapeWordView alloc] initWithFrame:CGRectMake(10, 20, 290, 100)];self.shape.lineColor = [UIColor redColor];self.shape.text = @"YouXianMing";self.shape.lineWidth = 0.5f;[self.shape buildView];[self.view addSubview:self.shape];
}- (void)scrollViewDidScroll:(UIScrollView *)scrollView {CGFloat offsetY = - scrollView.contentOffset.y;if (offsetY >= 0) {CGFloat percent = offsetY / 50;[self.shape percent:percent animated:NO];}
}@end
[控件] 将字符串转换成贝塞尔曲线并执行动画相关推荐
- Delphi字符串转换成代码
转载于网络: uses ComObj; 演示 Function calc(const expression : String):Integer; Var sc : OleVariant; begin ...
- Android kotlin 将Base64字符串转换成Bitmap,并在jetpack compose的Image控件中显示
Android kotlin 将Base64字符串转换成Bitmap 前言 代码 将Base64字符串转换成Bitmap 在jetpack compose的Image控件中显示Bitmap 完事 前言 ...
- VB 汉字字符串转换成拼音
以下这个函数可以将一个汉字字符串转换成拼音 Private Function Getpy(Txt) Dim d, TmpTxt Dim wzcode, i, TmpWZ, a, b, ii Set d ...
- Visual Basic 2005 - 如何将色彩字符串转换成 Color 结构
Visual Basic 2005 - 如何将色彩字符串转换成 Color 结构 之前有读者询问,如何将字符串转换成色彩,当时我们建议可以利用 ColorDialog 来让用户选取颜色.不过这样做 ...
- 程序员面试题精选100题(17)-把字符串转换成整数[算法]
题目:输入一个表示整数的字符串,把该字符串转换成整数并输出.例如输入字符串"345",则输出整数345. 分析:这道题尽管不是很难,学过C/C++语言一般都能实现基本功能,但不同程 ...
- 剑指Offer(Java版):把字符串转换成整数
2019独角兽企业重金招聘Python工程师标准>>> 题目:实现一个函数 stringToInt,实现把字符串转换成整数这个功能,不能使用 atoi 或者其他类似的库函数. 题目解 ...
- 程序员编程艺术第三十 三十一章 字符串转换成整数,通配符字符串匹配
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 第三十~ ...
- 字符串转换成整数,带通配符的字符串匹配
之前本一直想写写神经网络算法和EM算法,但写这两个算法实在需要大段大段的时间,而平时上班,周末则跑去北大教室自习看书(顺便以时间为序,说下过去半年看过的自觉还不错的数学史方面的书:<数理统计学简 ...
- php将sql语句识别成字符串,ASP_把字符串转换成数据库SQL语句格式,复制代码 代码如下:'把字符串 - phpStudy...
把字符串转换成数据库SQL语句格式 复制代码 代码如下: '把字符串转换成数据库SQL语句格式 '------------------------------------------------- F ...
- 字符串转换成整数,字符串匹配问题
本文转自csdn大神v_JULY_v的博客 地址: http://blog.csdn.net/v_july_v/article/details/9024123 阅读心得:自己原先想得太天真了... 第 ...
最新文章
- 成功的产品 = 做得好 + 卖得好
- JAVA碰撞检测无效_碰撞检测不适用于Pygame中的精灵
- .net利用程序集的GUID解决程序只能运行一次的问题
- Github for Windows使用介绍
- bag文件加载及可视化显示
- Okhttp 与 Retrofit的简单介绍及两者间的联系
- P2604 [ZJOI2010]网络扩容
- Zookeeper Client简介
- java编程删除文本框_Java获取和删除Word文本框中的表格
- Winhex手动恢复删除数据
- jquery Chosen使用
- 数学知识都是计算机,数学在计算机的作用
- React `controlled` 及 `uncontrolled` 组件
- python基础练习之【求三角形周长和面积】
- MCGS实现按键按一次按下,再按一次弹起
- c语言.jpg图片转成数组_如何把pdf图片转成jpg?快看高手私藏实用的技巧
- unity ,color组件
- about_Execution_Policies
- 11、CSS3选择器及属性
- 【漫画科普】什么是POL?什么是全光?