动画写名字

作者: Evan叶少 | 来源:发表于2016-07-04 17:59 被阅读273次

动画:CAAnimation使用

使用动画来写名字!

技术点 { 

// 先导进#import 头文件 使用CAShapeLayer结合UIBezierPath画圆,然后再结合CAAnimatio执行画圆动画 }

//  shapeLayer.lineJoin = kCALineJoinBevel;

//  lineJoin:要使用的接合类型

//  kCGLineJoinMiter//  接合点为尖角。这是默认的接合类型。

//  kCGLineJoinBevel

//  接合点为斜角

//  kCGLineJoinRound//  接合点为圆角

实例:

#import "ViewController.h"

#import@interface ViewController ()

@property (nonatomic, strong) CALayer *penLayer;

@property (nonatomic, weak) CAShapeLayer *shapeLayer;

@property (nonatomic, weak) UITextField *nameTitleFile;

@property (nonatomic, weak) UIButton *determineButton;

@end

@implementation ViewController

#pragma mark - Control

- (CAShapeLayer *)shapeLayer

{

if (!_shapeLayer)

{

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];

shapeLayer.frame = self.view.bounds;

shapeLayer.geometryFlipped = YES;//翻转

shapeLayer.strokeColor = [[UIColor blackColor] CGColor];

shapeLayer.fillColor = nil;

shapeLayer.lineWidth = 3.0f;

shapeLayer.lineJoin = kCALineJoinBevel;

self.shapeLayer = shapeLayer;

[self.view.layer addSublayer:shapeLayer];

}

return _shapeLayer;

}

- (UITextField *)nameTitleFile

{

if (!_nameTitleFile)

{

UITextField *nameTitleFile = [[UITextField alloc] init];

nameTitleFile.frame = CGRectMake(10, 64, [UIScreen mainScreen].bounds.size.width - 20, 44);

nameTitleFile.layer.borderWidth = 0.5f;

nameTitleFile.layer.borderColor = [UIColor grayColor].CGColor;

nameTitleFile.placeholder = @"请输入名称";

nameTitleFile.font = [UIFont systemFontOfSize:13];

self.nameTitleFile = nameTitleFile;

[self.view addSubview:nameTitleFile];

}

return _nameTitleFile;

}

- (UIButton *)determineButton

{

if (!_determineButton)

{

UIButton *determineButton = [[UIButton alloc] init];

determineButton.frame = CGRectMake(10, 118, [UIScreen mainScreen].bounds.size.width - 20, 44);

[determineButton setTitle:@"确定" forState:UIControlStateNormal];

[determineButton setBackgroundColor:[UIColor redColor]];

[determineButton addTarget:self action:@selector(determineButtonClick) forControlEvents:UIControlEventTouchUpInside];

self.determineButton = determineButton;

[self.view addSubview:determineButton];

}

return _determineButton;

}

#pragma mark - LfileCycle

- (void)viewDidLoad

{

[super viewDidLoad];

self.title = @"名字签写";

self.nameTitleFile.delegate = self;

self.determineButton;

}

#pragma mark - Touch Event

- (void)determineButtonClick

{

[self setupTextLayer];

[self startAnimation];

}

#pragma mark - setupTextLayer

- (void)setupTextLayer

{

if (self.shapeLayer != nil)

{

[self.penLayer removeFromSuperlayer];

[self.shapeLayer removeFromSuperlayer];

self.shapeLayer = nil;

self.penLayer = nil;

}

// 曲线动画的path

CGMutablePathRef letters = CGPathCreateMutable();

// 文字格式和大小

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 46.0, NULL);

NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:

(__bridge id)font, kCTFontAttributeName,nil];

// 文字

NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.nameTitleFile.text

attributes:attrs];

// 参考线

CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);

// CTLineGetGlyphRuns:构成线的ctrun对象。

CFArrayRef runArray = CTLineGetGlyphRuns(line);

// 每次运行

for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)

{

// 获取运行的字体,

// CTRunRef:运行参考

CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);

CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);

// 对于运行中的每个符号

// CTRunGetGlyphCount:运行获得字形计数

for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)

{

// 获取字形和字形数据

CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);// 范围

CGGlyph glyph;// 雕文

CGPoint position;// 位置

CTRunGetGlyphs(run, thisGlyphRange, &glyph);

CTRunGetPositions(run, thisGlyphRange, &position);

// 获取轮廓路径

{

CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);

CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);

CGPathAddPath(letters, &t, letter);

CGPathRelease(letter);

}

}

}

CFRelease(line);

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointZero];

[path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];

CGPathRelease(letters);

CFRelease(font);

self.shapeLayer.path = path.CGPath;

self.shapeLayer.bounds = CGPathGetBoundingBox(path.CGPath);

UIImage *penImage = [UIImage imageNamed:@"noun_project_347_2.png"];

CALayer *penLayer = [CALayer layer];

penLayer.contents = (id)penImage.CGImage;

penLayer.anchorPoint = CGPointZero;

penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width, penImage.size.height);

[self.shapeLayer addSublayer:penLayer];

self.penLayer = penLayer;

}

#pragma mark - startAnimation

- (void)startAnimation

{

[self.shapeLayer removeAllAnimations];

[self.penLayer removeAllAnimations];

self.penLayer.hidden = NO;

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

pathAnimation.duration = 10.0;

pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];

pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];

[self.shapeLayer addAnimation:pathAnimation forKey:@"strokeEnd"];

CAKeyframeAnimation *penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

penAnimation.duration = 10.0;

penAnimation.path = self.shapeLayer.path;

penAnimation.calculationMode = kCAAnimationPaced;

penAnimation.delegate = self;

penAnimation.removedOnCompletion = NO;

penAnimation.fillMode = kCAFillModeForwards;

[self.penLayer addAnimation:penAnimation forKey:@"position"];

}

@end

githubDemo:https://github.com/EvanYeShao/Animation-write-name

相关文章

  • 动画写名字

    动画:CAAnimation使用 使用动画来写名字! 技术点 { // 先导进#import 头文件 使用CASh...

  • CSS-动画模块

    告诉系统需要执行哪个动画animation-name: XXOO; (名字可以随便写) 告诉系统动画持续的时长an...

  • 写名字

    写名字 七子 如红大家都说我性子很慢 身在北方, 构成话 七子 比如我有我来看红吗 我这矫情的手居然在长冻疮。 可...

  • 写名字

    早饭后,我执行工作,教哥哥写自己的名字。小妹妹的参与,实在是扰乱,便请奶奶带妹妹外出玩耍。 字是都能写出来,只是写...

  • 口碑和评分超越《你的名字。》,日本排名第一的最佳动画即将在国内上

    说到日本动画,很多人第一反应都会想到被称为“现象级动画”——《你的名字。》。 而这部动画虽然人气没有《你的名字。》...

  • 动画(css3)animation

    animation:动画名称 动画时间 运动曲线 何时开始 播放次数 是否反方向; 关于几个值,除了名字,动画时间...

  • 你的名字—彗星的爱情

    这是给没看懂《你的名字》的你写的一个我的观影体验,希望可以帮到你。 朋友商量说要去看一个日本动画片,名字叫...

  • 开学写名字

    第一天开学,发书。幼儿园的最后一个学期,终于长大了,下半年开始小学。 幼儿园要画上句号了,宝贝加油了!想想三年的幼...

  • 仿写动画

    在简书上看到daixunry一篇关于动画的文章,看到这个动画的时候我也很喜欢,就想着用Swift将它实现,后面集成...

  • CSS写动画

    loading .box{ width: 620px; height: 400px...

网友评论

    本文标题:动画写名字

    本文链接:https://www.haomeiwen.com/subject/tzthjttx.html