iOS 实现UILabel的跑马灯效果

github地址:https://github.com/EmyWong/PersonalUITech

项目新功能模块UILabel长度有限,想要完全看到字就需要有跑马灯效果。

代码

ScrollLabel.h

#import#define NUM_LABELS 2enum AutoScrollDirection {    AUTOSCROLL_SCROLL_RIGHT,    AUTOSCROLL_SCROLL_LEFT,};@interface ScrollLabel : UIScrollView{

UILabel *label[NUM_LABELS];

enum AutoScrollDirection scrollDirection;

float scrollSpeed;

NSTimeInterval pauseInterval;

int bufferSpaceBetweenLabels;

bool isScrolling;

}

@property(nonatomic) enum AutoScrollDirection scrollDirection;

@property(nonatomic) float scrollSpeed;

@property(nonatomic) NSTimeInterval pauseInterval;

@property(nonatomic) int bufferSpaceBetweenLabels;

// normal UILabel properties

@property(nonatomic,retain) UIColor *textColor;

@property(nonatomic, retain) UIFont *font;

- (void) readjustLabels;

- (void) setText: (NSString *) text;

- (NSString *) text;

- (void) scroll;

@end

ScrollLabel.h

#import "ScrollLabel.h"

#define LABEL_BUFFER_SPACE 20  // pixel buffer space between scrolling label

#define DEFAULT_PIXELS_PER_SECOND 30

#define DEFAULT_PAUSE_TIME 0.5f

@implementation ScrollLabel

@synthesize pauseInterval;

@synthesize bufferSpaceBetweenLabels;

- (void) commonInit

{

for (int i=0; i< NUM_LABELS; ++i){

label[i] = [[UILabel alloc] init];

label[i].textColor = [UIColor whiteColor];

label[i].backgroundColor = [UIColor clearColor];

[self addSubview:label[i]];

}

scrollDirection = AUTOSCROLL_SCROLL_LEFT;

scrollSpeed = DEFAULT_PIXELS_PER_SECOND;

pauseInterval = DEFAULT_PAUSE_TIME;

bufferSpaceBetweenLabels = LABEL_BUFFER_SPACE;

self.showsVerticalScrollIndicator = NO;

self.showsHorizontalScrollIndicator = NO;

self.userInteractionEnabled = NO;

}

-(id) init

{

if (self = [super init]){

// Initialization code

[self commonInit];

}

return self;

}

- (id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super initWithCoder:aDecoder]) {

// Initialization code

[self commonInit];

}

return self;

}

- (id)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

// Initialization code

[self commonInit];

}

return self;

}

#if 0

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(voidvoid *)context

{

[NSThread sleepForTimeInterval:pauseInterval];

isScrolling = NO;

if ([finished intValue] == 1 && label[0].frame.size.width > self.frame.size.width){

[self scroll];

}

}

#else

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

{

isScrolling = NO;

if ([finished intValue] == 1 && label[0].frame.size.width > self.frame.size.width){

[NSTimer scheduledTimerWithTimeInterval:pauseInterval target:self selector:@selector(scroll) userInfo:nil repeats:NO];

}

}

#endif

- (void) scroll

{

// Prevent multiple calls

if (isScrolling){

//      return;

}

isScrolling = YES;

if (scrollDirection == AUTOSCROLL_SCROLL_LEFT){

self.contentOffset = CGPointMake(0,0);

}else{

self.contentOffset = CGPointMake(label[0].frame.size.width+LABEL_BUFFER_SPACE,0);

}

[UIView beginAnimations:@"scroll" context:nil];

[UIView setAnimationDelegate:self];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

[UIView setAnimationDuration:label[0].frame.size.width/(float)scrollSpeed];

if (scrollDirection == AUTOSCROLL_SCROLL_LEFT){

self.contentOffset = CGPointMake(label[0].frame.size.width+LABEL_BUFFER_SPACE,0);

}else{

self.contentOffset = CGPointMake(0,0);

}

[UIView commitAnimations];

}

- (void) readjustLabels

{

float offset = 0.0f;

for (int i = 0; i < NUM_LABELS; ++i){

[label[i] sizeToFit];

// Recenter label vertically within the scroll view

CGPoint center;

center = label[i].center;

center.y = self.center.y - self.frame.origin.y;

label[i].center = center;

CGRect frame;

frame = label[i].frame;

frame.origin.x = offset;

label[i].frame = frame;

offset += label[i].frame.size.width + LABEL_BUFFER_SPACE;

}

CGSize size;

size.width = label[0].frame.size.width + self.frame.size.width + LABEL_BUFFER_SPACE;

size.height = self.frame.size.height;

self.contentSize = size;

[self setContentOffset:CGPointMake(0,0) animated:NO];

// If the label is bigger than the space allocated, then it should scroll

if (label[0].frame.size.width > self.frame.size.width){

for (int i = 1; i < NUM_LABELS; ++i){

label[i].hidden = NO;

}

[self scroll];

}else{

// Hide the other labels out of view

for (int i = 1; i < NUM_LABELS; ++i){

label[i].hidden = YES;

}

// Center this label

CGPoint center;

center = label[0].center;

center.x = self.center.x - self.frame.origin.x;

label[0].center = center;

}

}

- (void) setText: (NSString *) text

{

// If the text is identical, don't reset it, otherwise it causes scrolling jitter

if ([text isEqualToString:label[0].text]){

// But if it isn't scrolling, make it scroll

// If the label is bigger than the space allocated, then it should scroll

if (label[0].frame.size.width > self.frame.size.width){

[self scroll];

}

return;

}

for (int i=0; i<NUM_LABELS; ++i){

label[i].text = text;

}

[self readjustLabels];

}

- (NSString *) text

{

return label[0].text;

}

- (void) setTextColor:(UIColor *)color

{

for (int i=0; i<NUM_LABELS; ++i){

label[i].textColor = color;

}

}

- (UIColor *) textColor

{

return label[0].textColor;

}

- (void) setFont:(UIFont *)font

{

for (int i=0; i<NUM_LABELS; ++i){

label[i].font = font;

}

[self readjustLabels];

}

- (UIFont *) font

{

return label[0].font;

}

- (void) setScrollSpeed: (float)speed

{

scrollSpeed = speed;

[self readjustLabels];

}

- (float) scrollSpeed

{

return scrollSpeed;

}

- (void) setScrollDirection: (enum AutoScrollDirection)direction

{

scrollDirection = direction;

[self readjustLabels];

}

- (enum AutoScrollDirection) scrollDirection

{

return scrollDirection;

}

@end

使用方法

代码篇:

autoScrollLabel.text = @"Hi Mom!  How are you?  I really ought to write more often.";

autoScrollLabel.textColor = [UIColor blackColor];//默认白色

如果使用XIB,那么你要创建一个UIScrollView,然后更改它的Class类别为AutoScrollLabel。

当文字不超过ScrollView的大小时不会滚动!

推荐阅读更多精彩内容

  • 今天分享自己掌握的几种UIlabel ,大家有更多的label请分享给我哟 1:内边距,自适应的label ,这个...
    icc_tips阅读 1,791评论 1 2
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,155评论 0 3
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    Chris948阅读 874评论 1 1
  • 决定开始每天坚持写点东西,一是锻炼自己的思维,免得僵硬。二是也算记录一下生活吧。 最近身体里充满了负能量,并不知道...
    行走的使命阅读 226评论 0 0
  • 二毛上周末来上海修车,微信上前一天告诉我,然后我吐槽了他一番,哈哈 周六那天特别冷,两毛修车的地方在老闵行那边,好...
    佛右扯阅读 83评论 0 0
  • 之前,在看日剧《半泽直树》的时候,里面有几个场景让我印象非常深刻,这几个场景都是半泽与对手在谈判桌上的较量。尽管,...
    幻无名阅读 450评论 0 0
  • 今天有位曾经在日本留学的朋友说,当我在工作坊中谈及父母帮忙收拾行李的时候,他非常有共鸣。茶山是2006年去韩国的,...
    茶山阅读 520评论 0 1
  • 偶然,发现一个入口,一束光!
    浣秋阅读 103评论 0 0