最近看到APP中有滾動(dòng)新聞的條幅,想了下怎么實(shí)現(xiàn)的,于是試著自己寫了一個(gè),寫的不好還望見諒!
效果圖:
github地址:
代碼地址:https://github.com/kuyuxing/scrollBanners.git
個(gè)人實(shí)現(xiàn)思路:
以一個(gè)view作為底部, 然后再創(chuàng)建兩個(gè)label, label中的數(shù)據(jù)是根據(jù)數(shù)組中字符串進(jìn)行加載出來的,兩個(gè)label分別叫l(wèi)abel1 和 label2, 首先將label1 放在上面,label2放在label1的底部,然后設(shè)置一個(gè)uiview動(dòng)畫計(jì)算兩個(gè)label的frame, label內(nèi)容根據(jù)數(shù)組中的字符串進(jìn)行加載, 當(dāng)label1 滾動(dòng)到最上面后再view動(dòng)畫回調(diào)將label1放在label2底部 , 此時(shí)在 回調(diào)中在執(zhí)行一次 uiview動(dòng)畫, 兩個(gè)label回到了起初的位置,然后加入一個(gè)定時(shí)器,就可以實(shí)現(xiàn)循環(huán)的效果
主要代碼:
// 定時(shí)器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 repeats:1 block:^(NSTimer * _Nonnull timer) {
// view動(dòng)畫
[UIView animateWithDuration:1 animations:^{
self.label1.frame = CGRectMake(0, -15, 150, 15);
self.label2.frame = CGRectMake(0, 0, 150, 15);
} completion:^(BOOL finished) {
// 回調(diào)計(jì)算frame 和 根據(jù)cout 計(jì)算數(shù)組字符串
self.label1.frame = CGRectMake(0, 15, 150, 15);
if (self.count < self.array.count - 1) {
self.count ++;
self.label1.text = self.array[self.count];
}else{
self.count = 0;
self.label1.text = self.array[self.count];
}
// 延時(shí)1秒后再次調(diào)用view動(dòng)畫
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1 animations:^{
self.label2.frame = CGRectMake(0, -15, 150, 15);
self.label1.frame = CGRectMake(0, 0, 150, 15);
} completion:^(BOOL finished) {
self.label2.frame = CGRectMake(0, 15, 150, 15);
if (self.count < self.array.count - 1) {
self.count ++;
self.label2.text = self.array[self.count];
}else{
self.count = 0;
self.label2.text = self.array[self.count];
}
}];
});
}];
}];
全部代碼:就全部貼過來了~
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, weak)UILabel *label1;
@property(nonatomic, weak)UILabel *label2;
// 字符串?dāng)?shù)組
@property(nonatomic, strong)NSArray *array;
// 記錄數(shù)組中字符串位置
@property(nonatomic, assign)NSInteger count;
// 定時(shí)器
@property(nonatomic, strong)NSTimer *timer;
@end
@implementation ViewController
- (NSArray *)array{
if (_array == nil) {
_array = @[@"我就想說:還有誰(shuí)?",@"我可以一直殺",@"國(guó)服第一JS",@"我一賤,你就笑"];
}
return _array;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// 定時(shí)器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 repeats:1 block:^(NSTimer * _Nonnull timer) {
// view動(dòng)畫
[UIView animateWithDuration:1 animations:^{
self.label1.frame = CGRectMake(0, -15, 150, 15);
self.label2.frame = CGRectMake(0, 0, 150, 15);
} completion:^(BOOL finished) {
// 回調(diào)計(jì)算frame 和 根據(jù)cout 計(jì)算數(shù)組字符串
self.label1.frame = CGRectMake(0, 15, 150, 15);
if (self.count < self.array.count - 1) {
self.count ++;
self.label1.text = self.array[self.count];
}else{
self.count = 0;
self.label1.text = self.array[self.count];
}
// 延時(shí)1秒后再次調(diào)用view動(dòng)畫
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1 animations:^{
self.label2.frame = CGRectMake(0, -15, 150, 15);
self.label1.frame = CGRectMake(0, 0, 150, 15);
} completion:^(BOOL finished) {
self.label2.frame = CGRectMake(0, 15, 150, 15);
if (self.count < self.array.count - 1) {
self.count ++;
self.label2.text = self.array[self.count];
}else{
self.count = 0;
self.label2.text = self.array[self.count];
}
}];
});
}];
}];
self.timer = timer;
// 開啟定時(shí)器
[timer setFireDate:[NSDate distantPast]];
[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
// 關(guān)閉定時(shí)器
[self.timer invalidate];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// bgView
UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 150, 15)];
bgView.layer.masksToBounds = YES;
bgView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:bgView];
// 11111
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 15)];
self.label1 = label1;
self.label1.font = [UIFont systemFontOfSize:12];
label1.text = self.array[0];;
label1.backgroundColor = [UIColor redColor];
[bgView addSubview:label1];
// 22222
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 150, 15)];
self.label2 = label2;
self.label2.font = [UIFont systemFontOfSize:12];
label2.backgroundColor = [UIColor yellowColor];
label2.text = self.array[1];
[bgView addSubview:label2];
self.count = 1;
}
然后就基本實(shí)現(xiàn)了功能,有點(diǎn)粗糙.抱歉!~
剛學(xué)會(huì)github上傳代碼然后就上傳了一份,哈哈,挺開心的!~
github地址:
代碼地址:https://github.com/kuyuxing/scrollBanners.git
祝大家開心每一天(*^__^*) 嘻嘻……