iOS中跑马灯效果

.h 文件

#import

#define TEXTCOLOR [UIColor colorWithRed:76.0/255.0 green:76.0/255.0 blue:76.0/255.0 alpha:1];

#define TEXTFONTSIZE 14

@interface LSPaoMaView : UIView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString*)title;

- (void)start;//开始跑马

- (void)stop;//停止跑马

@end

.m文件

​#import "LSPaoMaView.h"

@implementation LSPaoMaView

{

CGRect rectMark1;//标记第一个位置

CGRect rectMark2;//标记第二个位置

NSMutableArray* labelArr;

NSTimeInterval timeInterval;//时间

BOOL isStop;//停止

}

- (instancetype)initWithFrame:(CGRect)frame title:(NSString*)title

{

self = [super initWithFrame:frame];

if (self) {

//

title = [NSString stringWithFormat:@"  %@  ",title];//间隔

timeInterval = [selfdisplayDurationForString:title];

//        self.backgroundColor = [UIColor blackColor];

self.clipsToBounds = YES;

//

UILabel* textLb = [[UILabelalloc] initWithFrame:CGRectZero];

textLb.textColor = TEXTCOLOR;

textLb.font = [UIFontboldSystemFontOfSize:TEXTFONTSIZE];

textLb.text = title;

//计算textLb大小

CGSize sizeOfText = [textLb sizeThatFits:CGSizeZero];

rectMark1 = CGRectMake(0, 0, sizeOfText.width, self.bounds.size.height);

//        rectMark2 = CGRectMake(rectMark1.origin.x+rectMark1.size.width, 0, sizeOfText.width, self.bounds.size.height);

rectMark2 = CGRectMake(rectMark1.origin.x+rectMark1.size.width, 0, sizeOfText.width, self.bounds.size.height);

textLb.frame = rectMark1;

[self addSubview:textLb];

labelArr = [NSMutableArrayarrayWithObject:textLb];

//判断是否需要reserveTextLb

BOOL useReserve = sizeOfText.width > frame.size.width ? YES : NO;

if (useReserve) {

//alloc reserveTextLb ...

UILabel* reserveTextLb = [[UILabel alloc] initWithFrame:rectMark2];

reserveTextLb.textColor = TEXTCOLOR;

reserveTextLb.font = [UIFont boldSystemFontOfSize:TEXTFONTSIZE];

reserveTextLb.text = title;

[self addSubview:reserveTextLb];

[labelArr addObject:reserveTextLb];

[self paomaAnimate];

}

}

returnself;

}

- (void)paomaAnimate{

if (!isStop) {

//

UILabel* lbindex0 = labelArr[0];

UILabel* lbindex1 = labelArr[1];

[UIViewtransitionWithView:selfduration:timeIntervaloptions:UIViewAnimationOptionCurveLinearanimations:^{

//

lbindex0.frame = CGRectMake(-rectMark1.size.width, 0, rectMark1.size.width, rectMark1.size.height);

lbindex1.frame = CGRectMake(lbindex0.frame.origin.x+lbindex0.frame.size.width, 0, lbindex1.frame.size.width, lbindex1.frame.size.height);

} completion:^(BOOL finished) {

//

lbindex0.frame = rectMark2;

lbindex1.frame = rectMark1;

[labelArrreplaceObjectAtIndex:0withObject:lbindex1];

[labelArrreplaceObjectAtIndex:1withObject:lbindex0];

[self paomaAnimate];

}];

}

}

- (void)start{

isStop = NO;

UILabel* lbindex0 = labelArr[0];

UILabel* lbindex1 = labelArr[1];

lbindex0.frame = rectMark2;

lbindex1.frame = rectMark1;

[labelArrreplaceObjectAtIndex:0withObject:lbindex1];

[labelArrreplaceObjectAtIndex:1withObject:lbindex0];

[selfpaomaAnimate];

}

- (void)stop{

isStop = YES;

}

- (NSTimeInterval)displayDurationForString:(NSString*)string {

return string.length/5;

//    return MIN((float)string.length*0.06 + 0.5, 5.0);

}

@end

你可能感兴趣的:(iOS中跑马灯效果)