文字上下滾動(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