自定义标签,可以对整个文本或字母应用动画。
由Yalantis制作。
灵感来源于Dribble上的此项目
作为中国,您还可以直接将蛇夫座的所有源文件直接添加到您的项目中。
YALLabel
,使用#import "YALLabel.h"
。YALProgressAnimatingLayer
是一个CAShapeLayer
的子类,用于控制带有进度的动画。该特性在您调用[layer allowProgressToControlAnimations]
之前是禁用的,之后该层的duration
和timeOffset
属性将传递给添加到该层的任何动画。因此,您可以通过传递给progress
属性(介于0.f到1.f之间)的值来控制添加到层的动画。`YALProgressAnimatingLayer`的遮罩与层的类型相同。
YALTextLayer
是YALProgressAnimatingLayer
的子类,用于显示由UIBezierPath
实例组成的数组,作为YALProgressAnimatingLayer
的子层。您可以访问并操作每个字母子层。YALTextLayer
使用形状的默认边界框作为子层的遮罩来构建子层。
YALLabel
是一个自定义标签,由三个`YALTextLayer`实例组成,用于绘制文本的背景填充、描边和填充。
在Storyboard中放置一个UIView
,并将其类设置为YALLabel
,并配置fontName
、fontSize
、text
、strokeWidth
和颜色。
您可以在视图控制器中包含#import "YALLabel.h"
并从代码中创建它
self.yalLabel = [[YALLabel alloc] initWithFrame:frame];
self.yalLabel.text = @"AnyText";
self.yalLabel.fontName = @"FontName";
self.yalLabel.fontSize = 60.f;
self.yalLabel.fillColor = [UIColor redColor];
self.yalLabel.backgroundFillColor = [UIColor blackColor];
self.yalLabel.strokeColor = [UIColor blackColor];
self.yalLabel.strokeWidth = 1.2f;
在`self.yalLabel`被绘制后,您可以添加任何动画到您想要的任何子层。
示例:将填充动画添加到遮罩,如示例所示,但仅针对第一个字母:别忘了导入 YALPathFillAnimation.h
。
YALProgressAnimatingLayer *firstLetter = [self.yalLabel.fillLayer.sublayers firstObject];
CABasicAnimation *fillAnimation = [YALPathFillAnimation animationWithPath:fillLayer.mask.path andDirectionAngle:0];
fillAnimation.duration = 3.0;
[firstLetter.mask addAnimation:fillAnimation forKey:@"fillAnimation"];
您还可以使用进度来动画化图层
YALProgressAnimatingLayer *secondLetter = self.yalLabel.fillLayer.sublayers[1];
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"];
colorAnimation.fromValue = (id)[UIColor redColor].CGColor;
colorAnimation.toValue = (id)[UIColor blueColor].CGColor;
[secondLetter allowProgressToControlAnimations];
[secondLetter addAnimation:colorAnimation forKey:@"colorAnimation"];
secondLetter.progress = 0.f;
然后当您需要更新进度时
YALProgressAnimatingLayer *secondLetter = self.yalLabel.fillLayer.sublayers[1];
secondLetter.progress = value;
The MIT License (MIT)
Copyright © 2015 Yalantis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.