iOS-跑马灯效果

iOS中跑马灯实现由两种一种是通过UIView的动画,第二种是通过UIScrollView实现,最简单的通过UIView的线性动画即可实现:

- (void)scrollAnimation {
    CGRect mainRect = [[UIScreen mainScreen] bounds];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, mainRect.size.width, 100)];
    bgView.backgroundColor = [UIColor redColor];
    [self.view addSubview:bgView];
    self.scrollText = @"FlyElephant--简书博客--//www.greatytc.com/users/24da48b2ddb3/latest_articles";
    CGFloat width = [self textWidth:self.scrollText];
    self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,60,width,30)];
    self.testLabel.backgroundColor = [UIColor clearColor];
    self.testLabel.text = self.scrollText;
    [bgView addSubview:self.testLabel];
    [self linearAnimation];
}

- (void)linearAnimation {
    CGRect frame = self.testLabel.frame;
    [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.testLabel.frame = CGRectMake(-frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
    } completion:^(BOOL finished) {
        CGRect mainRect = [[UIScreen mainScreen] bounds];
        CGFloat width = [self textWidth:self.scrollText];
        self.testLabel.frame = CGRectMake(mainRect.size.width, 60, width, 30);
        [self linearAnimation];
    }];
}

- (CGFloat)textWidth:(NSString *)text {
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} context:nil].size;
    return size.width;
}

效果:

FlyElephant.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一.简介 跑马灯其实就是一串文字的轮播,实现的方式有很多种。这里我只介绍一种,通过CADisplayLink与UI...
    Sunshine_Boys阅读 2,547评论 0 1
  • 一个简单的跑马灯效果,话不多说,直接看代码建一个UIView类 到此,一个跑马灯的效果完成,demo地址
    风外杏林香阅读 859评论 0 2
  • 跑马灯效果在移动客户端中多用于“公告”、“消息”等功能的展示,本次demo以一种简易的实现方法(UILabel...
    fantasy_wan阅读 3,624评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 每到秋天每次洗完头发以后每次妈妈就会唠叨:“你就不能把你头发捋一捋啊,断头发都捋出来地板上就不会有这么多碎头发了啊...
    吐槽女与苹果糖阅读 216评论 0 0