ios自动滚动UILabel,跑马灯,有两种滚动方式
来源:互联网 发布:nba2k16数据 编辑:程序博客网 时间:2024/06/14 08:33
主要代码:
KJMarqueeLabel.h
#import <UIKit/UIKit.h>typedef NS_ENUM(NSUInteger, KJMarqueeLabelType) { KJMarqueeLabelTypeLeft = 0,//向左边滚动 KJMarqueeLabelTypeLeftRight = 1,//先向左边,再向右边滚动};@interface KJMarqueeLabel : UILabel@property(nonatomic,unsafe_unretained)KJMarqueeLabelType marqueeLabelType;@property(nonatomic,unsafe_unretained)CGFloat speed;//速度@property(nonatomic,unsafe_unretained)CGFloat secondLabelInterval;@property(nonatomic,unsafe_unretained)NSTimeInterval stopTime;//滚到顶的停止时间@end
KJMarqueeLabel.m
#import "KJMarqueeLabel.h"@interface KJMarqueeLabel (){ NSTimer *_timer; UIScrollView *_scrollV; UILabel *_label1; CGSize _scrollViewCcontentSize; CGFloat x; UILabel *_label2; BOOL _isRight;}@end@implementation KJMarqueeLabel- (void)awakeFromNib { [super awakeFromNib]; [self commonInit];}-(instancetype)init { if (self = [super init]) { [self commonInit]; } return self;}- (instancetype)initWithCoder:(NSCoder *)coder{ self = [super initWithCoder:coder]; if (self) { [self commonInit]; } return self;}-(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self commonInit]; } return self;}-(void)commonInit{ self.marqueeLabelType = KJMarqueeLabelTypeLeft; self.secondLabelInterval = 100; self.speed = 0.7f; self.stopTime = 1.5f;}-(UILabel *)comnInitLabel:(UILabel *)label { label = [[UILabel alloc]initWithFrame:self.bounds]; label.text = self.text; label.font = self.font; label.textColor = self.textColor; label.lineBreakMode = NSLineBreakByWordWrapping; return label;}-(void)drawTextInRect:(CGRect)rect { CGFloat height = self.bounds.size.height; CGFloat width = self.bounds.size.width; x = 0; _label1 = [self comnInitLabel:_label1]; if (self.marqueeLabelType == KJMarqueeLabelTypeLeft) { _label2 = [self comnInitLabel:_label2]; } CGSize size = [_label1 sizeThatFits:CGSizeMake(MAXFLOAT,height)]; //如果字符串的宽度小于或等于自身的宽度 if (size.width <= width) { [super drawTextInRect:rect]; return; } _scrollV = [[UIScrollView alloc]initWithFrame:self.bounds]; if (self.marqueeLabelType == KJMarqueeLabelTypeLeft) { _scrollViewCcontentSize = CGSizeMake(size.width+width+self.secondLabelInterval, size.height); }else { _scrollViewCcontentSize = CGSizeMake(size.width, size.height); } _scrollV.contentSize = _scrollViewCcontentSize; [self addSubview:_scrollV]; CGRect rect1 = _label1.frame; rect1.size.width = size.width; _label1.frame = rect1; [_scrollV addSubview:_label1]; if (self.marqueeLabelType == KJMarqueeLabelTypeLeft) { CGRect rect2 = _label2.frame; rect2.size.width = width; rect2.origin.x = size.width + self.secondLabelInterval; _label2.frame = rect2; [_scrollV addSubview:_label2]; } [self Timer];}-(void)Timer { if (_timer) { [_timer invalidate]; _timer = nil; } _timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(refreshMarqueeLabelFrame) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];}-(void)FistTimer { if (_timer) { [_timer invalidate]; _timer = nil; } _timer = [NSTimer scheduledTimerWithTimeInterval:self.stopTime target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];}-(void)dealloc { if (_timer) { [_timer invalidate]; _timer = nil; }}-(void)delayMethod { [self Timer];}- (void)refreshMarqueeLabelFrame { if (self.marqueeLabelType == KJMarqueeLabelTypeLeft) { x += self.speed; if (x > _scrollViewCcontentSize.width - _scrollV.bounds.size.width) { [self FistTimer]; x = 0; } [_scrollV setContentOffset:CGPointMake(x, 0)]; }else { if (_isRight) { x -= self.speed; }else { x += self.speed; } if (x > _scrollViewCcontentSize.width - _scrollV.bounds.size.width) { [self FistTimer]; _isRight = YES; return; } if (x <= 0) { [self FistTimer]; _isRight = NO; } [_scrollV setContentOffset:CGPointMake(x, 0)]; }}@end
阅读全文
1 0
- ios自动滚动UILabel,跑马灯,有两种滚动方式
- TextView 自动滚动(跑马灯)
- android开发TextView自动滚动,跑马灯显示方式
- UILabel自动滚动
- Android:TextView 自动滚动(跑马灯)
- TextView文字横向自动滚动(跑马灯)
- 关于TextView的跑马灯滚动方式
- ios 垂直方向跑马灯 循环滚动
- 滚动跑马灯问题
- Android文字跑马灯控件(文本自动滚动控件)
- Android文字跑马灯控件(文本自动水平滚动)
- Android TextView文字横向自动滚动(跑马灯)
- Android TextView文字横向自动滚动(跑马灯)
- TextView跑马灯效果,也就是,自动滚动文字的效果。
- Android TextView文字横向自动滚动(跑马灯)
- Android TextView文字横向自动滚动(跑马灯)
- Android TextView文字横向自动滚动(跑马灯)
- Android TextView文字横向自动滚动(跑马灯)
- uvalive2531一种分配模型(训练指南)p369
- 在成都58同城上打广告效果怎么样?
- 关于SAP---SAP新人或是准备进入SAP领域的新人必看!
- 预处理
- P1025 数的划分
- ios自动滚动UILabel,跑马灯,有两种滚动方式
- SQL 分组数据进行批量更新
- CDH集群添加HUE组件
- CodeForces
- Android设计模式之(14)----模板模式
- ue4中导入substance designer sbsar格式材质
- Leetcode--50. Pow(x, n)
- lambda表达式及jdk异步
- 学习记录