iOS滚动label显示效果

关于滚动 label 效果,大家应该都不陌生,在移动设备寸土寸金的显示屏中可以显示更多的内容,今天给大家带来一个滚动 label 效果实现的一个框架:SGAnimationLabel;

GitHub:SGAnimationLabel

简介:支持文字,大小,滚动速度,开始,结束滚动自定义
talk is cheap,show me code
具体代码:
使用:

    SGAnimationLabel *dynamicLabel = [[SGAnimationLabel   alloc]initWithFrame:CGRectMake(60, 100, 200, 30)];
    dynamicLabel.text = @"我不想说再见,不说再见,越长大越孤单";
    dynamicLabel.textColor = [UIColor orangeColor];
    dynamicLabel.font = [UIFont systemFontOfSize:24];
    dynamicLabel.speed = 0.2;
    [self.view addSubview:dynamicLabel];
    [dynamicLabel startAnimation];

SGAnimationLabel.h


#import 
@interface SGAnimationLabel : UIView
@property(nonatomic, strong) NSString *text;
@property(nonatomic, strong) UIColor *textColor;
@property(nonatomic, strong) UIFont *font;
@property(nonatomic, assign) CGFloat speed;//0.1~0.5  默认是0.3
/*!
 *
 * @abstract start animation
 */
- (void)startAnimation;
/*!
 *
 * @abstract stop animation
 */
- (void)stopAnimation;
@end

SGAnimationLabel.m

#import "SGAnimationLabel.h"
#define kDynamicLabelAnimationKey @"DynamicLabelAnimation"
@interface SGAnimationLabel()
@property (nonatomic ,strong)UILabel *animationLabel;
@end
@implementation SGAnimationLabel
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.speed = 0.3;
        [self initAnimationLabel];
        [self addNotification];
    }
    return self;
}
- (void)initAnimationLabel{
    _animationLabel = [[UILabel alloc]init];
    _animationLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_animationLabel];
    CAShapeLayer* maskLayer = [CAShapeLayer layer];
    maskLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.layer.mask = maskLayer;
}
- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAnimation) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
#pragma mark -
#pragma mark - set mothods
- (void)setText:(NSString *)text{
    self.animationLabel.text = text;
    [self.animationLabel sizeToFit];
}
- (void)setTextColor:(UIColor *)textColor{
    self.animationLabel.textColor = textColor;
}
- (void)setFont:(UIFont *)font{
    self.animationLabel.font = font;
    [self.animationLabel sizeToFit];
    CGRect frame = self.frame;
    if (frame.size.height < font.lineHeight) {
        frame.size.height = font.lineHeight;
        self.frame = frame;
    }
}
#pragma mark -
#pragma mark - start or stop animation
- (void)startAnimation{
    if ([self.animationLabel.layer animationForKey:kDynamicLabelAnimationKey] || self.animationLabel.frame.size.width <= self.frame.size.width) {
        return;
    }
    CGFloat lenth = self.animationLabel.frame.size.width + self.frame.size.width;
    CAKeyframeAnimation* keyFrame = [CAKeyframeAnimation animation];
    keyFrame.keyPath = @"transform.translation.x";
    keyFrame.values = @[@(0), @(-self.animationLabel.frame.size.width)];
    keyFrame.repeatCount = NSIntegerMax;
    keyFrame.duration = lenth * self.speed / 10;
    keyFrame.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [self.animationLabel.layer addAnimation:keyFrame forKey:kDynamicLabelAnimationKey];
}
- (void)stopAnimation{
    if ([self.animationLabel.layer animationForKey:kDynamicLabelAnimationKey]) {
        [self.animationLabel.layer removeAnimationForKey:kDynamicLabelAnimationKey];
    }
}
#pragma mark -
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
@end

希望大家能够喜欢...

你可能感兴趣的:(iOS滚动label显示效果)