美文网首页
iOS 跑马灯

iOS 跑马灯

作者: 燃烧的蔬菜C | 来源:发表于2018-04-24 16:50 被阅读0次

简易跑马灯效果,代码简单易懂,适合初学者

/** 新建文件,继承于UIView  .h文件代码如下*/

#import

@interfaceTTMarqueeView :UIView

/**

 *  跑马灯内容

 */

@property (nonatomic, strong) NSString *marqueeText;

/**

 *  关闭定时器

 */

- (void)closeTimer;

@end

/** .m实现文件 */

#import "TTMarqueeView.h"

#import "UIView+add.h"

#define kMarqueeLeftPadding    40

// 屏幕高度

#define kScreenH [UIScreen mainScreen].bounds.size.height

// 屏幕宽度

#define kScreenW [UIScreen mainScreen].bounds.size.width

@interface TTMarqueeView ()

{

    UILabel    *_marqueeL;

    NSTimer    *_timer;

}

@end

@implementation TTMarqueeView

- (instancetype)initWithFrame:(CGRect)frame

{

    if(self== [superinitWithFrame:frame]) {

        [selfsetupviews];

    }

    return self;

}

- (void)setupviews

{

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];

    UIImageView *speakerImg = [UIImageView new];

    speakerImg.image= [UIImageimageNamed:@"home_speaker"];

    CGSizesize = speakerImg.image.size;

    speakerImg.frame=CGRectMake(10, (self.frame.size.height- size.height) /2, size.width, size.height);

    [selfaddSubview:speakerImg];

    UIView*marqueeBgView = [UIViewnew];

    marqueeBgView.backgroundColor = [UIColor clearColor];

    marqueeBgView.frame = CGRectMake(CGRectGetMaxX(speakerImg.frame) + 10, 0, self.frame.size.width - CGRectGetMaxX(speakerImg.frame), self.frame.size.height);

    marqueeBgView.clipsToBounds=YES;

    [selfaddSubview:marqueeBgView];

    _marqueeL = ({

        UILabel*label = [UILabelnew];

        label.text=@"广播";

        label.textColor = [UIColor whiteColor];

        label.font = [UIFont systemFontOfSize:13];

        label.frame=CGRectMake(0,0, marqueeBgView.frame.size.width, marqueeBgView.frame.size.height);

        [marqueeBgViewaddSubview:label];

        label;

    });

}

- (void)closeTimer

{

    if(_timer) {

        [_timerinvalidate];

        _timer=nil;

    }

}

- (void)setMarqueeText:(NSString*)marqueeText

{

    _marqueeText= marqueeText;

    _marqueeL.text= marqueeText;

    [_marqueeL sizeToFit];

    [self closeTimer];

    _marqueeL.left = kScreenW - kMarqueeLeftPadding - 10;

    _marqueeL.centerY = self.height / 2;

    _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(resetMarqueeFrame) userInfo:nil repeats:YES];

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

}

- (void)resetMarqueeFrame

{

    _marqueeL.left = _marqueeL.left - 0.2;

    if (_marqueeL.right < 0) {

        _marqueeL.left = kScreenW - kMarqueeLeftPadding - 10;

    }

}

@end

Demo地址:https://github.com/UsernameHwb/TTMarquee/tree/master

PS:适用初学者,希望能帮助到需要的人,如有不对,欢迎指点!

相关文章

  • iOS:一用就上瘾的跑马灯视图

    iOS:一用就上瘾的跑马灯视图 iOS:一用就上瘾的跑马灯视图

  • iOS 跑马灯的实现

    介绍 我们一说起跑马灯第一个想到的就是:山寨机。接下来介绍的跑马灯和那个跑马灯是不一样滴。在iOS中,跑马灯是指l...

  • iOS简单音乐实现、React-Native完整项目、仿闲鱼京东

    iOS精选源码 iOS快速入手语音识别、听写、评测、播报 网络加载数据的过渡动画(仿简书网页) iOS 封装跑马灯...

  • iOS 跑马灯 一句话集成【转】

    原文地址:iOS 跑马灯 一句话集成至于为什么说一句话,看一下我在ViewController中 初始化跑马灯的D...

  • iOS 跑马灯

    简易跑马灯效果,代码简单易懂,适合初学者 /** 新建文件,继承于UIView .h文件代码如下*/ #impor...

  • iOS - 跑马灯

  • iOS 跑马灯

    几行代码实现跑马灯/ 一个Label搞定跑马灯要显示的文字text,宽度为width 。label.text = ...

  • iOS 跑马灯

    前言花里胡哨的UI,设计了花里胡哨的界面,然后我们苦逼的程序员就要去实现。 跑马灯效果有N多种实现,有通过动画实现...

  • iOS 跑马灯

    参考文章:https://www.jianshu.com/p/8f1f1b1ee814 使用第三方框架TXScro...

  • H5 div文字循环滚动

    实际上也叫跑马灯。说起跑马灯这个词,还是去年或前年写iOS时候用到的呢,不知道是哪位前辈兴得这个词儿。在h5里俗点...

网友评论

      本文标题:iOS 跑马灯

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