iOS开发之跑马灯

1.法一:
类似于弹幕,一个label的实现
- (void)viewDidLoad {
[super viewDidLoad];
[self addPaomaLabel];
}

- (void)addPaomaLabel {
self.annonLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, ScreenWidth-45, 30)];
self.annonLabel.text = @"哈哈哈哈带我逃跑吧的简书哈哈哈哈哈今年的目标要减肥哈哈哈";
self.annonLabel.font = [UIFont systemFontOfSize:14.0f];
self.annonLabel.textColor = [UIColor grayColor];
self.annonLabel.numberOfLines = 1;
[self.annonLabel sizeToFit];

[self.backView addSubview:self.annonLabel];
[self.backView sendSubviewToBack:self.annonLabel];

}

  - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
    self.annonLabel.center = CGPointMake(self.view.bounds.size.width +self.annonLabel.bounds.size.width/2, self.annonLabel.center.y);
    [self anmationForNoticeLabel];

}

-(void)anmationForNoticeLabel{
self.annonLabel.center = CGPointMake(self.view.bounds.size.width +     self.annonLabel.bounds.size.width/2, self.annonLabel.center.y);
[UIView animateWithDuration:10 animations:^{
    self.annonLabel.center = CGPointMake(self.annonLabel.center.x - self.annonLabel.bounds.size.width-self.view.bounds.size.width, self.annonLabel.center.y);
} completion:^(BOOL finished) {
    if (finished) {
        if (_isAnimation) {
          [self anmationForNoticeLabel];
        }
    }
}];

}

2.法二:两个label组成的跑马灯,前面文字消失,后面文字出现
- (void)viewDidLoad {
[super viewDidLoad];
[self addTwoLabelPaoMa];
}

  - (void)addTwoLabelPaoMa {
self.annonLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, ScreenWidth-45, 30)];
self.annonLabel.text = @"今天看着六六感觉又胖了,昨天做的内外驱虫,早上大便也没看到虫子,卫生情况良好哈哈哈哈哈";
self.annonLabel.font = [UIFont systemFontOfSize:12.0f];
self.annonLabel.numberOfLines = 1;
[self.annonLabel sizeToFit];

self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.annonLabel.frame.origin.x+self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y, self.annonLabel.frame.size.width, self.annonLabel.frame.size.height)];
self.secondLabel.text = self.annonLabel.text;
self.secondLabel.font = self.annonLabel.font;
self.secondLabel.numberOfLines = 1;
[self.secondLabel sizeToFit];

[self.backView addSubview:self.annonLabel];
[self.backView addSubview:self.secondLabel];

[self.backView sendSubviewToBack:self.annonLabel];
[self.backView sendSubviewToBack:self.secondLabel];

[self addAnimation];

}

- (void)addAnimation{
CGRect scrollFrame = self.annonLabel.frame;
CGRect secondFrame = self.secondLabel.frame;

// 10为动画时间,我这里model文字比较长,你们可自行设置短一点

[UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  self.annonLabel.frame = CGRectMake(-         self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y,      self.annonLabel.frame.size.width,      self.annonLabel.frame.size.height);

// 40是文字较多,所以在消失后出现时设置间距为40,文字较少可直接设置为0

     self.secondLabel.frame = CGRectMake(40, self.secondLabel.frame.origin.y, self.secondLabel.frame.size.width, self.secondLabel.frame.size.height);
} completion:^(BOOL finished) {
        self.annonLabel.frame = scrollFrame;
     self.secondLabel.frame = secondFrame;
           [self addAnimation];
}];
}

2835490-1486de942bae3525.gif
法二实现效果.gif

法二demo地址:
http://download.csdn.net/download/simona_1973/9670022

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值