文字上下滾動
項目中可能會用到單條數(shù)據(jù)需要上下滾動顯示载庭,下面是一個非常簡單的封裝Demo,可以根據(jù)自己項目進(jìn)行自定義修改靖榕。
效果如下:
效果圖.gif
Demo下載
項目代碼
/** 數(shù)據(jù) */
@property (nonatomic, copy) NSArray *dataArr;
/** 當(dāng)前Label */
@property (nonatomic, strong) UILabel *currentLab;
/** 下一個label */
@property (nonatomic, strong) UILabel *nextLab;
/** 定時器 */
@property (nonatomic, strong) NSTimer *timer;
/** 當(dāng)前下標(biāo) */
@property (nonatomic, assign) NSInteger currentIndex;
主要實現(xiàn)思路:通過兩個Lab進(jìn)行動畫操作實現(xiàn)上下滾動再進(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ù)實際情況進(jìn)行自定義界面操作茁计,實現(xiàn)原理不變
GitHub地址 :https://github.com/qiuyubude/RollTextView
簡書地址:http://www.reibang.com/p/8744fd855eed
掘金地址:https://juejin.im/post/5de4d1d35188250f9c2472b4