iOS文本的跑馬顯示

跑馬是大家都很熟悉的一種現(xiàn)象(嘿嘿嘿~~~),這個(gè)不做多解釋。我們會(huì)在眾多的app或者網(wǎng)頁(yè)上看到跑馬顯示的效果。
近期項(xiàng)目需要實(shí)現(xiàn)這樣一個(gè)功能:一個(gè)展示文字的控件顯示一段文字(例如一個(gè)標(biāo)題),實(shí)現(xiàn)在左右方向上的循環(huán)滾動(dòng)。為了實(shí)現(xiàn)這樣一個(gè)小功能,我就封裝了一個(gè)控件,用于實(shí)現(xiàn)其功能。
其具體的實(shí)現(xiàn)過(guò)程及代碼如下:
自定義視圖EntertainingDiversionsView繼承于UIView

EntertainingDiversionsView .h

#import <UIKit/UIKit.h>

@interface EntertainingDiversionsView : UIView

/**
 顯示字體的顏色   默認(rèn)黑色
 */
@property(nonatomic,strong)UIColor *show_textColor;

/**
 顯示字體的大小  默認(rèn)17
 */
@property(nonatomic,assign)CGFloat show_textFontSize;

/**
 顯示字體的背景顏色  默認(rèn)白色
 */
@property(nonatomic,strong)UIColor *show_textBgColor;

/**
 滾動(dòng)時(shí)間間隔 多長(zhǎng)時(shí)間滾動(dòng)一個(gè)像素 默認(rèn)是0.01 值越小滾動(dòng)的速度越快
 */
@property(nonatomic,assign)NSTimeInterval show_speedInterval;


/**
 跑馬燈初始化

 @param frame 位置
 @param runSpeedInteral 滾動(dòng)速度間隔(以像素點(diǎn)為單位)
 @return EntertainingDiversionsView
 */
- (instancetype)initWithFrame:(CGRect)frame runSpeedInteral:(CGFloat)runSpeedInteral;
/**
 運(yùn)動(dòng)狀態(tài)下顯示的文字設(shè)置

 @param showText showText description
 */
-(void)showTextInRuning:(NSString *)showText;

/**
 開(kāi)始滾動(dòng)
 */
-(void)run;

/**
 暫停滾動(dòng)
 */
-(void)stop;
@end

EntertainingDiversionsView .m

#import "EntertainingDiversionsView.h"

@interface EntertainingDiversionsView ()

@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)UILabel *showTextLabel;

@end
@implementation EntertainingDiversionsView

-(instancetype)initWithFrame:(CGRect)frame runSpeedInteral:(CGFloat)runSpeedInteral{
    EntertainingDiversionsView *entertainingDiversionsView =[self initWithFrame:frame];
    _show_speedInterval = runSpeedInteral;
    
    [self initTimer];
    return entertainingDiversionsView;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;//切除超出部分
    }
    return self;
}

-(UILabel *)showTextLabel{
    if (_showTextLabel == nil) {
        _showTextLabel = [[UILabel alloc]init];
        [self addSubview:_showTextLabel];
    }
    return _showTextLabel;
}

-(void)showTextInRuning:(NSString *)showText{
    self.showTextLabel.text = showText;
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
    CGSize size = [self.showTextLabel.text sizeWithAttributes:attribute];
    self.showTextLabel.frame=CGRectMake(self.frame.size.width, 0, size.width, self.frame.size.height);
}

#pragma mark------初始化計(jì)時(shí)器,并設(shè)置計(jì)時(shí)器輪式切換的速度
-(void)initTimer{
    if (_timer == nil) {
        if (_show_speedInterval == 0) {
            _timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(eventWithTimeChange) userInfo:nil repeats:YES];
        }else{
            _timer = [NSTimer timerWithTimeInterval:_show_speedInterval target:self selector:@selector(eventWithTimeChange) userInfo:nil repeats:YES];
        }
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
        [_timer fire];
    }
}

#pragma mark------位置改變
-(void)eventWithTimeChange{
    CGRect rect = self.showTextLabel.frame;
    rect.origin.x -= 1;
    //設(shè)置rect.origin.x < rect.origin.x < -rect.size.width - rect.size.width/4的時(shí)候重置frame,是為了內(nèi)容完全展示完畢的時(shí)候 有一個(gè)時(shí)間間隔 這個(gè)根據(jù)個(gè)人情況而定
    if (rect.origin.x < -rect.size.width - rect.size.width/4) {
        rect.origin.x = self.frame.size.width;
    }
    self.showTextLabel.frame = rect;
}

#pragma mark------屬性設(shè)置
-(void)setShow_textColor:(UIColor *)show_textColor{
    
    self.showTextLabel.textColor = show_textColor;
    
}

-(void)setShow_textBgColor:(UIColor *)show_textBgColor{
    
    self.showTextLabel.backgroundColor = show_textBgColor;
    
}

-(void)setShow_textFontSize:(CGFloat)show_textFontSize{
    
    self.showTextLabel.font = [UIFont boldSystemFontOfSize:show_textFontSize];
    
}
//開(kāi)始滾動(dòng)
-(void)run{
    [_timer setFireDate:[NSDate distantPast]];
}
//停止?jié)L動(dòng)
-(void)stop{
    [_timer setFireDate:[NSDate distantFuture]];
}
//釋放占用
-(void)dealloc{
    [_timer invalidate];
    _timer = nil;
}
@end

以上是其自定義過(guò)程,其使用也是相當(dāng)?shù)暮?jiǎn)單,只需要實(shí)現(xiàn):

entertainingDiversionsView = [[EntertainingDiversionsView alloc]initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 40) runSpeedInteral:0.01];
[entertainingDiversionsView showTextInRuning:@"北京教培師訓(xùn)網(wǎng)絡(luò)科技股份有限公司"];
[self.view addSubview:entertainingDiversionsView];

另外我們也可以設(shè)置一下跑馬字體的大小,顏色,背景顏色信息

entertainingDiversionsView.show_textColor = [UIColor redColor];
entertainingDiversionsView.show_textFontSize = 20;
entertainingDiversionsView.show_textBgColor = [UIColor greenColor];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,521評(píng)論 25 708
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,903評(píng)論 22 665
  • 淺拷貝:只復(fù)制指向?qū)ο蟮闹羔槪粡?fù)制引用對(duì)象本身。在另一個(gè)地方用同一個(gè)指針引用該對(duì)象。 深拷貝:復(fù)制引用對(duì)象本身...
    March_Cullen閱讀 205評(píng)論 0 1
  • “我對(duì)加班不深?lèi)和唇^,但也不歡欣鼓舞,因?yàn)檫@是每個(gè)人的選擇,所有的一切在于你的心態(tài)”; “我見(jiàn)過(guò)所有成功的人,他們...
    需要了解閱讀 262評(píng)論 0 0
  • 一座橋斷了幾千年 湖水漣漣 醉了游人眼 西子湖畔沉淀了千古傳說(shuō) 雷峰塔穿著古裝 佛音嘹亮 看著斷橋 書(shū)寫(xiě)絕世滄桑 ...
    雨燕江南閱讀 391評(píng)論 1 10