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
相关推荐
2501_9160137418 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184120 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张20 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
wjm04100621 小时前
ios面试八股文
ios·面试
张较瘦_1 天前
[论文阅读] 人工智能 + 软件工程 | 大模型破局跨平台测试!LLMRR让iOS/安卓/鸿蒙脚本无缝迁移
论文阅读·人工智能·ios
m0_641031051 天前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky1 天前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
白玉cfc2 天前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
低调小一2 天前
iOS 开发入门指南-HelloWorld
ios
2501_915918412 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview