iOS滚动横幅

作者: 哭与行 | 来源:发表于2017-07-27 12:07 被阅读238次

    最近看到APP中有滚动新闻的条幅,想了下怎么实现的,于是试着自己写了一个,写的不好还望见谅!

    效果图:

    github地址:

    代码地址:https://github.com/kuyuxing/scrollBanners.git

    个人实现思路:

    以一个view作为底部, 然后再创建两个label, label中的数据是根据数组中字符串进行加载出来的,两个label分别叫label1 和 label2, 首先将label1 放在上面,label2放在label1的底部,然后设置一个uiview动画计算两个label的frame, label内容根据数组中的字符串进行加载, 当label1 滚动到最上面后再view动画回调将label1放在label2底部 , 此时在 回调中在执行一次 uiview动画, 两个label回到了起初的位置,然后加入一个定时器,就可以实现循环的效果

    主要代码:

    // 定时器

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 repeats:1 block:^(NSTimer * _Nonnull timer) {

    // view动画

    [UIView animateWithDuration:1 animations:^{

    self.label1.frame = CGRectMake(0, -15, 150, 15);

    self.label2.frame = CGRectMake(0, 0, 150, 15);

    } completion:^(BOOL finished) {

    // 回调计算frame 和 根据cout 计算数组字符串

    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];

    }

    // 延时1秒后再次调用view动画

    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;

    // 字符串数组

    @property(nonatomic, strong)NSArray *array;

    // 记录数组中字符串位置

    @property(nonatomic, assign)NSInteger count;

    // 定时器

    @property(nonatomic, strong)NSTimer *timer;

    @end

    @implementation ViewController

    - (NSArray *)array{

    if (_array == nil) {

    _array = @[@"我就想说:还有谁?",@"我可以一直杀",@"国服第一JS",@"我一贱,你就笑"];

    }

    return _array;

    }

    - (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    // 定时器

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4 repeats:1 block:^(NSTimer * _Nonnull timer) {

    // view动画

    [UIView animateWithDuration:1 animations:^{

    self.label1.frame = CGRectMake(0, -15, 150, 15);

    self.label2.frame = CGRectMake(0, 0, 150, 15);

    } completion:^(BOOL finished) {

    // 回调计算frame 和 根据cout 计算数组字符串

    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];

    }

    // 延时1秒后再次调用view动画

    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;

    // 开启定时器

    [timer setFireDate:[NSDate distantPast]];

    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];

    }

    -(void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];

    // 关闭定时器

    [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;

    }

    然后就基本实现了功能,有点粗糙.抱歉!~

    刚学会github上传代码然后就上传了一份,哈哈,挺开心的!~

    github地址:

    代码地址:https://github.com/kuyuxing/scrollBanners.git

    祝大家开心每一天(*^__^*) 嘻嘻……

    相关文章

      网友评论

        本文标题:iOS滚动横幅

        本文链接:https://www.haomeiwen.com/subject/ufdzwttx.html