123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #import "TCScrollTextView.h"
- #import "NSAttributedString+Size.h"
- @interface TCScrollTextView ()
- @end
- @implementation TCScrollTextView
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- [self initUI];
- }
- return self;
- }
- - (void)initUI {
-
- _contentLabel = [[UILabel alloc] init];
- _contentLabel.backgroundColor = [UIColor clearColor];
- _contentLabel.textColor = [UIColor whiteColor];
- [self addSubview:_contentLabel];
-
- //超出部分不显示
- self.layer.masksToBounds = YES;
- //设置默认值
- self.speed = 0.5;
- //解决进入后台然后回来就停了
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationDidBecomeActiveNotification object:nil];
- }
- - (void)startAnimation {
- [self setNeedsLayout];
- }
- - (void)setText:(NSString *)text {
- _text = text;
- _contentLabel.text = text;
- //根据内容得到_contentLabel的frame
- [_contentLabel sizeToFit];
- //居中显示
- CGRect frame = _contentLabel.frame;
- frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
- _contentLabel.frame = frame;
- }
- //NSAttributedString *abString = [_richText attributedString];
- //float richTextLableWidth = [abString getWidthWithHeight:25] + 20;
- //_richTextLab.rtFrame = CGRectMake(10, 5, richTextLableWidth, 20);
- - (void)setTextAttributedString:(NSAttributedString *)abString {
- //_text = text;
- //_contentLabel.text = text;
- _contentLabel.attributedText = abString;
- //根据内容得到_contentLabel的frame
- [_contentLabel sizeToFit];
- //居中显示
- CGRect frame = _contentLabel.frame;
- frame.origin.y = (self.bounds.size.height - frame.size.height)/2;
- _contentLabel.frame = frame;
- //float richTextLableWidth = [abString getWidthWithHeight:25] + 20;
-
- }
- - (void)setFont:(UIFont *)font {
- _font = font;
- _contentLabel.font = font;
- }
- - (void)setSpeed:(CGFloat)speed {
- _speed = speed;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
-
- CGFloat sizeWidth = CGRectGetWidth(_contentLabel.frame);
-
- [self.contentLabel.layer removeAnimationForKey:@"keyFrame"];
- //如果文字能够显示完全 则不用滚动显示
- if (sizeWidth<= self.bounds.size.width) {
- return;
- }
-
- //添加帧动画 实现滚动效果 其实就是改变x的值
- CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
- keyFrame.keyPath = @"transform.translation.x";
- keyFrame.values = @[@(0), @(-sizeWidth + self.bounds.size.width)];
- keyFrame.repeatCount = NSIntegerMax;
- keyFrame.autoreverses = NO;
- keyFrame.duration = self.speed * self.contentLabel.text.length*0.8;
- // keyFrame.duration = 12+(1/self.speed) * self.contentLabel.text.length*0.1;
- NSLog(@"layoutSubviews _speed:%f duration:%f lenth:%lu",_speed,keyFrame.duration,self.contentLabel.text.length);
- //滚动速度改变 可以实现滚动速度由慢变快或者由快变慢
- // keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithControlPoints:0 :0 :0.5 :0.5]];
- [self.contentLabel.layer addAnimation:keyFrame forKey:@"keyFrame"];
- }
- @end
|