当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS在固定的label上动态显示所有文字

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

照例先看下效果图:

思路

创建一个view 作为所有内容的父控件, 并且添加到上面一个 label, 作为显示文字的载体

uilabel* contentlabel = [[uilabel alloc] init];
[contentlabel sizetofit];
contentlabel.backgroundcolor = [uicolor clearcolor]; _contentlabel = contentlabel; [self addsubview:self.contentlabel];

给内容viewlayer添加一个mask层, 并且设置其范围为整个viewbounds, 这样就让超出view的内容不会显示出来

cashapelayer* masklayer = [cashapelayer layer];
masklayer.path = [uibezierpath bezierpathwithrect:self.bounds].cgpath;
self.layer.mask = masklayer;

label添加动画

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];

使用方法

// 创建
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];

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网