源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS在固定的label上动态显示所有文字

  • 时间:2022-10-05 05:56 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS在固定的label上动态显示所有文字
[b]照例先看下效果图:[/b] [img]http://files.jb51.net/file_images/article/201610/2016101484009541.gif?201691484023[/img] [b]思路[/b] 创建一个[code]view [/code]作为所有内容的父控件, 并且添加到上面一个 [code]label[/code], 作为显示文字的载体
UILabel* contentLabel = [[UILabel alloc] init];
[contentLabel sizeToFit];
contentLabel.backgroundColor = [UIColor clearColor]; _contentLabel = contentLabel; [self addSubview:self.contentLabel];
给内容[code]view[/code]的[code]layer[/code]添加一个[code]mask[/code]层, 并且设置其范围为整个[code]view[/code]的[code]bounds[/code], 这样就让超出[code]view[/code]的内容不会显示出来
CAShapeLayer* maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.mask = maskLayer;
给[code]label[/code]添加动画
CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
keyFrame.keyPath = @"transform.translation.x";
keyFrame.values = @[@(0), @(-space), @(0)];
keyFrame.repeatCount = NSIntegerMax;
keyFrame.duration = self.speed * self.contentLabel.text.length;
keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
keyFrame.delegate = self;
[self.contentLabel.layer addAnimation:keyFrame forKey:nil];
[b]使用方法[/b]
// 创建
CFDynamicLabel* testLabel = [[CFDynamicLabel alloc] initWithFrame:CGRectMake(100, 300, 180, 21)];
// 设置滚动速度
testLabel.speed = 0.6;
[self.view addSubview:testLabel];
// 设置基本属性
testLabel.text = @"我不想说再见,不说再见,越长大越孤单";
testLabel.textColor = [UIColor yellowColor];
testLabel.font = [UIFont systemFontOfSize:23];
testLabel.backgroundColor = [UIColor grayColor];
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部