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
相关推荐
aiopencode8 小时前
局域网设备抓包怎么做?用 TraceEagle 抓电视、游戏机和 IoT 流量
后端·ios
奇牙coding9 小时前
GPT-5.5 报 401 invalid_api_key 怎么办?换密钥前按 4 层排查请求头、项目权限和端点
数据库·gpt·ios·ai
szp200510 小时前
中文字幕烧进视频后整片空白:CoreText 低报行高,导出照样成功
ios·swift
二流小码农10 小时前
HarmonyOS开发助手,0代码让元服务从开发到上架
android·ios·harmonyos
zhongben123_15 小时前
从0到1开发一款生鲜配送APP:核心技术难点与架构避坑指南
大数据·运维·人工智能·ios·架构·web app
zhongben_keji16 小时前
批发商城APP怎么做:价格引擎、授信台账与资金合规的设计要点
ios·小程序·web app
茶底世界之下17 小时前
夜景加胶片颗粒,先看天空:Awaking 两版真实导出
ios
2501_9151063219 小时前
iOS 单元测试怎么跑?用 KXApp 在 VS Code 里跑 XCTest 和 Swift Testing
ide·vscode·ios·单元测试·个人开发·swift·敏捷流程
2501_9159214319 小时前
iOS 实时日志怎么看?KeyMob 的日志过滤、崩溃报告与符号化
android·ios·小程序·https·uni-app·iphone·webview
天蒙蒙亮19 小时前
打ios app包+“应用商城”发布
ios