跑馬是大家都很熟悉的一種現(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];