当前位置: 首页 > news >正文

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)frameconfig:(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;
}
http://www.lryc.cn/news/342613.html

相关文章:

  • MySQL的视图、存储过程、触发器
  • 【图像特征点匹配】
  • GZIPOutputStream JSON压缩
  • 毫米波雷达原理(含代码)(含ARS548 4D毫米波雷达数据demo和可视化视频)
  • 3.1 Gateway之路由请求和转发
  • 人脸识别开源算法库和开源数据库
  • Excel 中用于在一个范围中查找特定的值,并返回同一行中指定列的值 顺序不一样 可以处理吗
  • MySql-日期分组
  • 有哪些方法可以在运行时动态生成一个Java类?
  • JAVA两个线程交替打印实现
  • 【C语言】学习C语言
  • C 深入指针(2)
  • FileLink跨网文件交换,推动企业高效协作|半导体行业解决方案
  • 代码随想录day56 | 动态规划P16 | ● 583. ● 72. ● 编辑距离总结篇
  • ASP.NET网络在线考试系统
  • 天锐绿盾 | 办公加密系统,源代码防泄密、源代码透明加密、防止开发部门人员泄露源码
  • Day1| Java基础 | 1 面向对象特性
  • Spring 事务失效的几种情况
  • 【Linux 命令操作】如何在 Linux 中使用多行注释呢?
  • 【RPC】Dubbo接口测试
  • PVZ2 植物克僵尸【第二期】
  • libcity笔记:libcity/data/batch.py
  • 【Java EE】多线程(二)Thread 类与常用方法
  • AGV无人叉车 | 我们为什么要投资“智慧生产”
  • 【C++】滑动窗口:将x减到0的最小操作数
  • 运动控制“MC_MoveVelocity“功能块详细应用介绍
  • 9种单片机常用的软件架构
  • PyQt5中重要的概念:信号与槽
  • MacOS快速安装FFmpeg,并使用FFmpeg转换视频
  • docker部署nginx并配置https