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
相关推荐
搜狐技术产品小编20233 小时前
CAEmitterLayer:iOS 中创建炫酷粒子效果的魔法工具
macos·ios·objective-c·cocoa
00后程序员张1 天前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
咕噜签名分发冰淇淋1 天前
内测分发是什么?
ios
2501_916007471 天前
Transporter App 使用全流程详解:iOS 应用 ipa 上传工具、 uni-app 应用发布指南
android·ios·小程序·https·uni-app·iphone·webview
白玉cfc1 天前
【OC】单例模式
开发语言·ios·单例模式·objective-c
Digitally1 天前
比较 iPhone:全面比较 iPhone 17 系列
android·ios·iphone
2501_915909061 天前
HTTPS 错误解析,常见 HTTPS 抓包失败、443 端口错误与 iOS 抓包调试全攻略
android·网络协议·ios·小程序·https·uni-app·iphone
他们都不看好你,偏偏你最不争气2 天前
【iOS】UIViewController
开发语言·macos·ios·objective-c·cocoa
前端小超超2 天前
如何配置capacitor 打包的ios app固定竖屏展示?
前端·ios·web app
CocoaKier2 天前
AI让35岁程序员再次伟大
ios·微信小程序·aigc