美文网首页
iOS中跑马灯效果

iOS中跑马灯效果

作者: Youthfulless | 来源:发表于2016-11-30 11:04 被阅读108次

.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中跑马灯效果

    .h 文件 #import #define TEXTCOLOR [UIColor colorWithRed:76....

  • iOS中跑马灯效果小结

    时光过得好快,记忆中刚刚从春节返回没有多久,清明、五一已飞逝而过,眨眼已到盛夏季节。不过还好,济南这两年不算太热,...

  • Android 基础 UI 之 TextView

    一、显示富文本 效果图image 布局文件 逻辑代码 二、跑马灯效果 1. 横向跑马灯 效果图: 单个实现跑马灯:...

  • Kevin Learn Android:TextView

    一、显示富文本 效果图01.png 布局文件 逻辑代码 二、跑马灯效果 1. 横向跑马灯 效果图: 单个实现跑马灯...

  • TextView漏的知识点

    跑马灯效果

  • leetcode ascii码 汇总

    跑马灯效果

  • iOS 跑马灯效果

    一个简单的跑马灯效果,话不多说,直接看代码建一个UIView类 到此,一个跑马灯的效果完成,demo地址

  • iOS 跑马灯效果

    介绍 因业务需求,需要做一个跑马灯效果的公告栏,查了网上大部分制作原理,都不能满足也无需求,决定自己造轮子 效果 ...

  • iOS跑马灯效果

    一.简介 跑马灯其实就是一串文字的轮播,实现的方式有很多种。这里我只介绍一种,通过CADisplayLink与UI...

  • iOS 跑马灯效果

    - (void)viewDidLoad { [super viewDidLoad]; // Do any ...

网友评论

      本文标题:iOS中跑马灯效果

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