iOS 文字上下滾動(dòng)

文字上下滾動(dòng)

項(xiàng)目中可能會(huì)用到單條數(shù)據(jù)需要上下滾動(dòng)顯示,下面是一個(gè)非常簡(jiǎn)單的封裝Demo,可以根據(jù)自己項(xiàng)目進(jìn)行自定義修改。

效果如下:

效果圖.gif

Demo下載

項(xiàng)目代碼

/** 數(shù)據(jù) */
@property (nonatomic, copy) NSArray *dataArr;
/** 當(dāng)前Label */
@property (nonatomic, strong) UILabel *currentLab;
/** 下一個(gè)label */
@property (nonatomic, strong) UILabel *nextLab;
/** 定時(shí)器 */
@property (nonatomic, strong) NSTimer *timer;
/** 當(dāng)前下標(biāo) */
@property (nonatomic, assign) NSInteger currentIndex;

主要實(shí)現(xiàn)思路:通過(guò)兩個(gè)Lab進(jìn)行動(dòng)畫操作實(shí)現(xiàn)上下滾動(dòng)再進(jìn)行數(shù)據(jù)賦值操作

- (void)refreshData {

    if (self.currentIndex == self.dataArr.count - 1) {
       self.currentIndex = -1;
    }
    self.nextLab.text = self.dataArr[self.currentIndex + 1];
    self.currentIndex ++;
}

- (void)rollLab {
    
    [self refreshData];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.currentLab.frame = CGRectMake(0,  - 24, weakSelf.frame.size.width, 24);
        weakSelf.nextLab.frame = CGRectMake(0, 0, weakSelf.frame.size.width, 24);
    } completion:^(BOOL finished) {
        weakSelf.currentLab.text = weakSelf.nextLab.text;
        weakSelf.currentLab.frame = CGRectMake(0, 0, weakSelf.frame.size.width, 24);
        weakSelf.nextLab.frame = CGRectMake(0, 24, weakSelf.frame.size.width, 24);
    }];
}

可根據(jù)實(shí)際情況進(jìn)行自定義界面操作,實(shí)現(xiàn)原理不變

GitHub地址 :https://github.com/qiuyubude/RollTextView
簡(jiǎn)書地址:http://www.itdecent.cn/p/8744fd855eed
掘金地址:https://juejin.im/post/5de4d1d35188250f9c2472b4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容