可以自動切換子控件內容的View

這里是項目里需要用到一個類似的功能,然后我自己的一個想法.

需求

在tableViewcell上面有幾個label,有幾行數據,隔5s之后,把label顯示的數據換成下一組數據.


功能.gif

這個需要根據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

BBCyclingLabel


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容