iOS实现一个高性能的跑马灯

该跑马灯完全通过CATextLayer 实现,轻量级,并且通过

系统的位移动画实现滚动效果,避免了使用displaylink造成的性能瓶颈,使用系统动画,系统自动做了很多性能优化,实现更好的性能,并使用遮罩实现展示范围的限定

,实现跑马灯效果

//
//  LBMarqueeLayer.m
//  TEXT
//
//  Created by mac on 2024/4/28.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBMarqueeLayer.h"

@implementation LBMarqueeLayerConfig

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.velocity = 20;
        self.fontSize = 14;
        self.textColor = [UIColor darkGrayColor];
    }
    return self;
}

@end

@interface LBMarqueeLayer ()

@property (nonatomic, strong) CATextLayer *textLayer;

@property (nonatomic, strong) CALayer *maskLayer;

@property (nonatomic, strong) LBMarqueeLayerConfig *config;

@property (nonatomic, strong) CABasicAnimation *animation;

@end

@implementation LBMarqueeLayer

- (instancetype)initwithFrame:(CGRect)frame
                       config:(LBMarqueeLayerConfig *)config
{
    if ([super init]) {
        self.frame = frame;
        self.config = config;
        [self handleText];
        [self addSublayer:self.textLayer];
    }
    return self;
}

- (void)handleText
{
    CGFloat width = [self.config.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.config
    .fontSize]}].width;
    if (width > CGRectGetWidth(self.bounds)) {
        NSString *content = [NSString stringWithFormat:@"%@   %@", self.config.text, self.config.text];
        self.textLayer.string = content;
        CGFloat width = [content sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:self.config
                                                                            .fontSize]}].width;
        self.textLayer.frame = CGRectMake(0, 0, width, CGRectGetHeight(self.bounds));
        CGFloat toValue = [[NSString stringWithFormat:@"%@   ", self.config.text] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.config
                                                                                                                           .fontSize]}].width;
        self.animation.fromValue = @(0);
        self.animation.toValue = @(-toValue + 3);
        self.animation.duration = toValue/self.config.velocity;
        [self.textLayer addAnimation:self.animation forKey:@"animation"];
        self.masksToBounds = YES;
       // self.mask = self.maskLayer;
    } else {
        self.textLayer.string = self.config.text;
    }
}

#pragma mark - lazy load

- (CATextLayer *)textLayer
{
    if (!_textLayer) {
        _textLayer = [[CATextLayer alloc] init];
        _textLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        _textLayer.alignmentMode = kCAAlignmentLeft;
        _textLayer.fontSize = 14;
        _textLayer.foregroundColor = self.config.textColor.CGColor;
    }
    return _textLayer;
}

- (CALayer *)maskLayer
{
    if (!_maskLayer) {
        _maskLayer = [[CALayer alloc] init];
        _maskLayer.frame = self.bounds;
    }
    return _maskLayer;
}

- (CABasicAnimation *)animation
{
    if (!_animation) {
        _animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        _animation.repeatCount = NSIntegerMax;
    }
    return _animation;
}

@end
相关推荐
2401_8658548810 小时前
iOS应用想要下载到手机上只能苹果签名吗?
后端·ios·iphone
HackerTom1 天前
iOS用rime且导入自制输入方案
ios·iphone·rime
良技漫谈1 天前
Rust移动开发:Rust在iOS端集成使用介绍
后端·程序人生·ios·rust·objective-c·swift
2401_852403551 天前
高效管理iPhone存储:苹果手机怎么删除相似照片
ios·智能手机·iphone
星际码仔1 天前
【动画图解】是怎样的方法,能被称作是 Flutter Widget 系统的核心?
android·flutter·ios
emperinter1 天前
WordCloudStudio:AI生成模版为您的文字云创意赋能 !
图像处理·人工智能·macos·ios·信息可视化·iphone
关键帧Keyframe2 天前
音视频面试题集锦第 8 期
ios·音视频开发·客户端
pb82 天前
引入最新fluwx2.5.4的时候报错
flutter·ios
袁代码2 天前
Swift 开发教程系列 - 第4章:函数与闭包
ios·swift·ios开发
#摩斯先生3 天前
IOS 防截屏实现
ios·xcode