iOS实现一个高性能的跑马灯

server/2024/9/25 8:22:38/

该跑马灯完全通过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)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.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

http://www.ppmy.cn/server/23190.html

相关文章

NTP授时服务器(GPS授时器)在DCS系统应用

NTP授时服务器(GPS授时器)在DCS系统应用 前言 随着计算机和网络通信技术的飞速发展,各行业自动化系统数字化、网络化的时代已经到来。这一方面为各控制和信息系统之间的数据交换、分析和应用提供了更好的平台、另一方面对各种实时和历史数据…

SG-9101CG,2520有源晶振,扩展频率晶振

扩展频率晶振中的SG-9101CG,是一款小尺寸2520有源晶振。随着市场小型化、多功能、高信赖度的电子产品需求量大增,产品开发周期时间要求越来越短,传统的石英品振性能优异,但设计制造周期相对较长。SG-9101CG内置一个高稳定的有源晶…

python程序设计语言超详细知识总结

Python 首先 python 并不是简单,什么语言都有基础和高级之分,要想掌握一门语言,必须把高级部分掌握才行。 HelloWorld helloWorld.py print(hello, world)数据类型与变量 变量的数据类型数据类型描述变量的定义方式整数型 (int)整数&…

Django框架之Django安装与使用

一、Django框架下载 首先我们需要先确定好自己电脑上的python解释器环境,否则会导致后面项目所需要的库安装不了以及项目无法运行的问题。 要下载Django并开始使用它,你可以按照以下步骤进行: 1、安装Python 首先,确保你的计算…

掌握Java并发工具:Callable和Future实战技巧

Callable接口介绍 Callable vs Runnable 在Java中,Callable接口是一个返回结果并可能抛出异常的任务。它类似于Runnable接口,但有两个显著的不同: Callable的call()方法可以返回值。call()方法可以抛出受检查的异常。 import java.util.c…

Docker基本操作 Linux里边操作

docker镜像操作命令: docker images:查看所有镜像; docker rmi:删除镜像 后边可以跟镜像的名字或者id指定要删除的镜像; docker pull:拉取镜像; docker push:推送镜像到服务; docker save :打包镜像 后边有用法; docker load:加载镜像&…

微前端技术之Web Components

Web Component 是一套技术,允许你创建可重用定制的元素(它们的功能封装在你的代码之外)并且在你的web应用中使用它们。通俗来讲就是将部分可重用的代码抽离,封装成一个独立的组件,方便在其他地方引用。 Web components…

Shader实战(3):贴图像素化风格实现

话不多说,将以下shader赋给材质贴上贴图即可。 Shader "HQY/Shader2" //自己改名 {Properties{_Diffuse ("Diffuse", Color) (1,1,1,1)_MainTex ("MainTex", 2D) "white" {}_Specular("Specular", Color) (…