iOS 文字动画,文字逐个显示
2016-01-16 21:51
435 查看
随手记:
对于iOS文字动画,首先想到的应该是基于CoreText框架将文字每个都写出来,加上定时器给定每个字显示的时间,这是一种思维方式:
是不是还有另一种思维方式呢,答案是有的,只需在label上加一个白色的挡板就OK了,当然,别忘记了把挡板的anchorPoint变为(1,0.5),让它从左边开始变小:
demo下载地址:http://download.csdn.net/download/et295394330/9406943
对于iOS文字动画,首先想到的应该是基于CoreText框架将文字每个都写出来,加上定时器给定每个字显示的时间,这是一种思维方式:
#import "ViewController.h" #import <CoreText/CoreText.h> @interface ViewController () { NSMutableArray *labels; NSMutableArray *numArr; NSMutableArray *dataArr; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)buttonClick:(id)sender { UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; vi.backgroundColor = [UIColor whiteColor]; [self.view addSubview:vi]; CATextLayer *textLayer = [CATextLayer layer]; textLayer.frame = vi.bounds; textLayer.contentsScale = [UIScreen mainScreen].scale; [vi.layer addSublayer:textLayer]; textLayer.alignmentMode = kCAAlignmentJustified; textLayer.wrapped = YES; UIFont *font = [UIFont fontWithName:@"EuphemiaUCAS-Bold" size:12]; NSArray *familyNames = [UIFont familyNames]; for( NSString *familyName in familyNames ){ printf( "Family: %s \n", [familyName UTF8String] ); NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName]; for( NSString *fontName in fontNames ){ printf( "\tFont: %s \n", [fontName UTF8String] ); } } NSString *str = @"还是月西江更有意境:日暮江水远,入夜随风迁,秋叶乱水月..."; NSMutableAttributedString *string = nil; string = [[NSMutableAttributedString alloc] initWithString:str]; CFStringRef fontName = (__bridge CFStringRef)(font.fontName); CGFloat fontSize = font.pointSize; CTFontRef fontRef = CTFontCreateWithName(fontName, fontSize, NULL); NSDictionary *attribs = @{ (__bridge id)kCTForegroundColorAttributeName:(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)kCTFontAttributeName:(__bridge id)fontRef }; [string setAttributes:attribs range:NSMakeRange(0, str.length)]; dataArr = [NSMutableArray arrayWithObjects:(__bridge id _Nonnull)(fontRef),attribs,string,str,textLayer, nil]; numArr = [NSMutableArray array]; for (int i = 0; i < str.length; i++) { [numArr addObject:[NSNumber numberWithInt:i]]; [self performSelector:@selector(changeToBlack) withObject:nil afterDelay:0.1 * i]; } CFRelease(fontRef); } - (void)changeToBlack { CTFontRef fontRef = (__bridge CTFontRef)(dataArr[0]); NSMutableAttributedString *string = dataArr[2]; NSNumber *num = [numArr firstObject]; int y = [num intValue]; NSDictionary *attribs = dataArr[1]; attribs = @{ (__bridge id)kCTForegroundColorAttributeName:(__bridge id)[UIColor blackColor].CGColor, (__bridge id)kCTFontAttributeName:(__bridge id)fontRef }; [string setAttributes:attribs range:NSMakeRange(y, 1)]; if (numArr.count > 1) { [numArr removeObjectAtIndex:0]; } CATextLayer *textLayer = [dataArr lastObject]; textLayer.string = string; }
是不是还有另一种思维方式呢,答案是有的,只需在label上加一个白色的挡板就OK了,当然,别忘记了把挡板的anchorPoint变为(1,0.5),让它从左边开始变小:
#import "ViewController.h" @interface ViewController () { UIView *myView; BOOL isclick; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; label.text = @"我其实很喜欢你的,初音"; [self.view addSubview:label]; myView = [[UIView alloc] initWithFrame:CGRectMake(200, 100, 200, 50)]; myView.backgroundColor = [UIColor whiteColor]; myView.layer.anchorPoint = CGPointMake(1, 0.5); [self.view addSubview:myView]; } - (IBAction)buttonClick:(id)sender { if (!isclick) { [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ myView.bounds = CGRectMake(0, 0, 10, 50); } completion:^(BOOL finished) { }]; } else { [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ myView.bounds = CGRectMake(0, 0, 200, 50); } completion:^(BOOL finished) { }]; } isclick = !isclick; }
demo下载地址:http://download.csdn.net/download/et295394330/9406943
相关文章推荐
- ios学习day—1
- iOS截屏“闪现效果实现”
- ios如何处理声音
- iOS开发检测项目中哪个库包含了idfa
- iOS学习之路-超级猜图
- iOS 【Multithreading-GCD 同步/异步函数 和 串行/并发队列 的6种搭配使用及介绍】
- iOS盲人模式小结
- iOS调试Bug方式之——NSZombieEnabled(僵尸模式)
- GLKit Framework 浅析
- iOS:获取图片Alpha图片
- ios之TableViewCell重用机制避免反复显示问题
- ios 常见错误整理 持续更新
- ios nsnumber怎么转换成nsstring
- 苹果审核通过后包体大了200多MB!都是Xcode 7的错?
- iOS之 json数据解析
- 更新Windows ActiveX,Ios
- iOS--错误集锦--****is missing a [super ViewDidLoad]call
- iOS 8 自适应 Cell
- ios开发笔记
- 基于SnapKit写的自动计算行高开源库