这里是项目里需要用到一个类似的功能,然后我自己的一个想法.
需求
在tableViewcell上面有几个label,有几行数据,隔5s之后,把label显示的数据换成下一组数据.
这个需要根据cell的frame,label的高度和cell对应的数组的个数来计算每个cell中的label的个数和总共的页数
核心代码
- (void)setUpUI
{
if (_backGroundColor == nil) {
self.backgroundColor = [UIColor whiteColor];
}else{
self.backgroundColor = _backGroundColor;
}
//向下取整,每页个数
self.pageCount = floorf(self.frame.size.height / self.labelHeight);
//得到总共的页数
self.pageIndex = ceilf(self.dataSource.count / self.pageCount);
if (self.dataSource.count % self.pageCount != 0) {
self.pageIndex++;
}
NSLog(@"pageCount === %ld", self.pageCount);
NSLog(@"pageIndex === %ld", self.pageIndex);
for (int i = 0; i < self.pageCount; i ++) {
BBCyclingLabel* tempLabel = [self createLabelForAutoViewWithHeight:i * _labelHeight];
tempLabel.tag = (i + 1) * 100;
tempLabel.text = [self.dataSource objectAtIndex:i];
[self addSubview:tempLabel];
}
[self performSelector:@selector(toChangeselfSubviews) withObject:nil afterDelay:self.delayTime];
}
- (void)toChangeselfSubviews
{
//先让当前页+1
self.currentIndex++;
NSLog(@"changeTextView");
if (self.currentIndex < self.pageIndex) {
NSInteger count = self.currentIndex * self.pageCount;
// NSLog(@"currentIndex %ld %ld",self.currentIndex,count);
for (NSInteger i = count; i < self.pageCount + count; i ++) {
CFDynamicLabel* tempLabel = [self viewWithTag:(i - count + 1) * 100];
if (i < self.dataSource.count) {
tempLabel.text = [self.dataSource objectAtIndex:i];
}else{
tempLabel.text = @"";
}
}
}else{
self.currentIndex = 0;
NSInteger count = self.currentIndex * self.pageCount;
// NSLog(@"currentIndex %ld %ld",self.currentIndex,count);
for (NSInteger i = count; i < self.pageCount + count; i ++) {
CFDynamicLabel* tempLabel = [self viewWithTag:(i - count + 1) * 100];
tempLabel.text = [self.dataSource objectAtIndex:i];
}
}
[self performSelector:@selector(toChangeselfSubviews) withObject:nil afterDelay:self.delayTime];
}
在这里我需要每个cell上面放一个这样的View,所以没有用计时器,而是调用了系统的方法.
附上代码:
https://github.com/WWLJ/AutoChangeContentView/tree/master
代码中借用了俩位大神的关于label动画的代码
CFDynamicLabel