[控件] 将字符串转换成贝塞尔曲线并执行动画

简介:

将字符串转换成贝塞尔曲线并执行动画

部分开源代码支持:

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;
    }
    
    // 初始化layer
    self.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:text
                                                       withFont:font
                                                       maxWidth:self.bounds.size.width
                                                  textAlignment: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.bounds
                                                           style: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

目录
相关文章
|
7月前
|
存储 前端开发
canvas自定义绘制顺序解决遮挡问题
canvas自定义绘制顺序解决遮挡问题
90 0
An动画基础之散件动画原理与形状提示点
An动画基础之散件动画原理与形状提示点
707 0
An动画基础之散件动画原理与形状提示点
|
前端开发
Delphi绘图功能[2] —— 窗体的绘图属性、圆弧类图形、获取Canvas对象(ClientRect解析)
Delphi绘图功能[2] —— 窗体的绘图属性、圆弧类图形、获取Canvas对象(ClientRect解析)
323 0
Delphi绘图功能[2] —— 窗体的绘图属性、圆弧类图形、获取Canvas对象(ClientRect解析)
|
存储 图形学
|
JavaScript 开发者
动画-小球动画 flag 标识符的作用分析|学习笔记
快速学习动画-小球动画 flag 标识符的作用分析
133 0
动画-小球动画 flag 标识符的作用分析|学习笔记
|
JavaScript 开发者
动画-小球动画flag标识符的作用分析|学习笔记
快速学习动画-小球动画flag标识符的作用分析
81 0
动画-小球动画flag标识符的作用分析|学习笔记
|
图形学
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
411 0
Unity【DoTween】- 如何使Transform Tween动画序列可编辑
|
前端开发 C#
WPF使用Canvas绘制可变矩形
原文:WPF使用Canvas绘制可变矩形 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WANGYAN9110/article/details/38130661 1、问题以及解决办法 最近因为项目需要,需要实现一个位置校对的功能,大致的需求如下:有一个图片,有一些位置信息,但是位置信息可能和实际有些偏差,需要做简单调整,后面会对这张图片进行切割等,做些处理。
1648 0
|
C#
WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆)
原文:WPF 中使用附加属性,将任意 UI 元素或控件裁剪成圆形(椭圆) 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:http://blog.csdn.net/wpwalter/),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。
1234 0
|
C# 索引 容器
WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同时增加鼠标的悬停效果。
1737 0