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];
        self.pauseDuration = 3;
        self.blankString = @"  ";
    }
    return self;
}

@end

@interface LBMarqueeLayer () <CAAnimationDelegate>

@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.blankString, self.config.text];
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:content attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.config.fontSize]}];
        self.textLayer.string = attributedString;
        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, self.config.blankString] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:self.config
                                                                                                                            .fontSize]}].width;
        self.animation.fromValue = @(0);
        self.animation.toValue = @(-toValue);
        self.animation.duration = toValue/self.config.velocity;
        [self.textLayer addAnimation:self.animation forKey:@"animation"];
        self.masksToBounds = YES;
        } else {
            self.textLayer.string = self.config.text;
        }
    }
    
#pragma mark - animationDelegate
    
    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    {
        
        if (flag) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.config.pauseDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.textLayer addAnimation:self.animation forKey:@"animation"];
            });
        }
    }
    
#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"];
            if (!self.config.hasPause) {
                _animation.repeatCount = NSIntegerMax;
            }
            _animation.delegate = self;
        }
        return _animation;
    }
    
    @end

调用


- (LBMarqueeLayer *)textLayer
{
    if (!_textLayer) {
        LBMarqueeLayerConfig *config = [[LBMarqueeLayerConfig alloc] init];
        config.hasPause = YES;
        config.pauseDuration = 3;
        config.blankString = @"      ";
        config.text = @"这是一首非常好听的歌曲哈哈哈哈";
        _textLayer = [[LBMarqueeLayer alloc] initwithFrame:CGRectMake(100, 100, 200, 50) config:config];
        _textLayer.backgroundColor = [UIColor cyanColor].CGColor;
    }
    return _textLayer;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,让我来为您解答。 针对您提出的问题,我可以给您提供以下建议: 苹果的推送服务APNs(Apple Push Notification Service)是一种向iOS、watchOS、tvOS设备推送消息的服务,它可以让您的应用程序向设备发送提醒、警报和通知等信息。在实现iOS高性能消息推送时,我们可以使用最新的APNs HTTP/2接口进行开发。 APNs HTTP/2接口的优点在于可以通过单个TCP连接同时发送多个推送通知,提高了推送效率和性能。在进行iOS消息推送的服务端开发时,可以使用苹果提供的HTTP/2 API或者第三方库进行开发,例如使用Swift推荐的APNSwift库等。 在实现iOS消息推送的服务端开发时,需要注意以下几个方面: 1. 配置APNs证书和密钥:在使用APNs推送服务之前,需要在苹果开发者中心创建APNs证书和密钥,并将它们配置到服务端。 2. 实现HTTP/2协议:需要使用HTTP/2协议与APNs服务器进行通信,可以使用Swift中的NSURLSession或Alamofire等第三方库进行实现。 3. 封装推送消息:需要将推送消息封装成APNs要求的格式,包括消息体、标识符、过期时间等信息。 4. 处理推送响应:需要处理APNs服务器返回的推送响应,包括成功和失败的推送消息。 总之,实现iOS高性能消息推送需要深入了解APNs HTTP/2接口的使用和实现方式,同时需要掌握相关的技术和工具。希望这些信息能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值