iOS 实现跑马灯效果的方法示例
发布时间 - 2026-01-10 22:32:05 点击率:次在网页开发当中跑马灯是常用到的,用来显示通知等,在游戏开发当中也如此。

首先来看看效果图:
接下来就简单看看这效果是怎么实现的。
实现方法
1、首先我们从这个图片里面能联想到如果实现这个效果必然需要使用到动画,或者还有有用scrollView的思路,这里我是用的动画的方式实现的。
2、.h文件
自定义一个继承UIView的LGJAutoRunLabel类,在.h文件中:
@class LGJAutoRunLabel;
typedef NS_ENUM(NSInteger, RunDirectionType) {
LeftType = 0,
RightType = 1,
};
@protocol LGJAutoRunLabelDelegate <NSObject>
@optional
- (void)operateLabel: (LGJAutoRunLabel *)autoLabel animationDidStopFinished: (BOOL)finished;
@end
@interface LGJAutoRunLabel : UIView
@property (nonatomic, weak) id <LGJAutoRunLabelDelegate> delegate;
@property (nonatomic, assign) CGFloat speed;
@property (nonatomic, assign) RunDirectionType directionType;
- (void)addContentView: (UIView *)view;
- (void)startAnimation;
- (void)stopAnimation;
定义一个NS_ENUM用来判断自动滚动的方向,分别是左和右,声明一个可选类型的协议,用来在controller中调用并对autoLabel进行操作。声明对外的属性和方法。这里一目了然,主要的实现思路都集中在.m文件中。
3、.m文件
声明“私有”变量和属性:
@interface LGJAutoRunLabel()<CAAnimationDelegate>
{
CGFloat _width;
CGFloat _height;
CGFloat _animationViewWidth;
CGFloat _animationViewHeight;
BOOL _stoped;
UIView *_contentView;//滚动内容视图
}
@property (nonatomic, strong) UIView *animationView;//放置滚动内容视图
@end
初始化方法:
- (instancetype)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
_width = frame.size.width;
_height = frame.size.height;
self.speed = 1.0f;
self.directionType = LeftType;
self.layer.masksToBounds = YES;
self.animationView = [[UIView alloc] initWithFrame:CGRectMake(_width, 0, _width, _height)];
[self addSubview:self.animationView];
}
return self;
}
将滚动内容视图contentView添加到动画视图animationView上:
- (void)addContentView:(UIView *)view {
[_contentView removeFromSuperview];
view.frame = view.bounds;
_contentView = view;
self.animationView.frame = view.bounds;
[self.animationView addSubview:_contentView];
_animationViewWidth = self.animationView.frame.size.width;
_animationViewHeight = self.animationView.frame.size.height;
}
让animationView上的contentView自动滚动起来的主要方法在这儿,重点来了,就是这个- (void)startAnimation方法,看一下这个方法里面是怎么样实现的:
- (void)startAnimation {
[self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
_stoped = NO;
CGPoint pointRightCenter = CGPointMake(_width + _animationViewWidth / 2.f, _animationViewHeight / 2.f);
CGPoint pointLeftCenter = CGPointMake(-_animationViewWidth / 2, _animationViewHeight / 2.f);
CGPoint fromPoint = self.directionType == LeftType ? pointRightCenter : pointLeftCenter;
CGPoint toPoint = self.directionType == LeftType ? pointLeftCenter : pointRightCenter;
self.animationView.center = fromPoint;
UIBezierPath *movePath = [UIBezierPath bezierPath];
[movePath moveToPoint:fromPoint];
[movePath addLineToPoint:toPoint];
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.path = movePath.CGPath;
moveAnimation.removedOnCompletion = YES;
moveAnimation.duration = _animationViewWidth / 30.f * (1 / self.speed);
moveAnimation.delegate = self;
[self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
}
↘️首先先把animationView.layer上的动画移除掉,接下来就是要找到animationView\contentView的pointCenter这里把这个中点当做是animationView或者是contentView都行,因为这两个视图的frame是相等的,这步找左右中点的意义在于,完全显示出文字内容,因为可能会遇到那种比如文字太长了,view长度不够,不能完全显示出来文字的全部内容, 这里我们找到中点,也就相当于确定了内容视图要滑动的范围了,接下来根据起始方向的枚举值设置fromPoint和toPoint这里我们默认是从右向左滚动的。这里我们做动画设置,用到了贝塞尔曲线,我们设置UIBezierPath的起始位置就是fromPoint也就是屏幕右方(我们看不见)self.animationView.center。终止位置是屏幕左方toPoint此时animationView滚动的起始位置的首和终止位置的尾的距离正好是屏幕的宽度。这里我们使用CAKeyframeAnimation关键帧动画,moveAnimation.path = movePath.CGPath; ,moveAnimation.removedOnCompletion = YES;动画完成后就将动画移除,不保留最终的状态。 [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];将动画添加到animationView.layer上。
(这段是对上面代码的解释)
-------------------分割线-------------------
接下来的这个就是代理方法的实现了,当动画完成后悔调用LGJAutoRunLabelDelegate我们自定义的delegate方法。
- (void)stopAnimation {
_stoped = YES;
[self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (self.delegate && [self.delegate respondsToSelector:@selector(operateLabel:animationDidStopFinished:)]) {
[self.delegate operateLabel:self animationDidStopFinished:flag];
}
if (flag && !_stoped) {
[self startAnimation];
}
}
4、在controller中使用方法
主要的方法就是声明LGJAutoRunLabel实例,将代理设为自身,声明directionType默认为Left,在runLabel上创建label也就是我们看到的文字。其余方法一目了然了。
//MARK:- CreateAutoRunLabel
- (void)createAutoRunLabel {
LGJAutoRunLabel *runLabel = [[LGJAutoRunLabel alloc] initWithFrame:CGRectMake(0, 100, kWidth, 50)];
runLabel.delegate = self;
runLabel.directionType = LeftType;
[self.view addSubview:runLabel];
[runLabel addContentView:[self createLabelWithText:@"繁华声 遁入空门 折煞了梦偏冷 辗转一生 情债又几 如你默认 生死枯等 枯等一圈 又一圈的 浮图塔 断了几层 断了谁的痛直奔 一盏残灯 倾塌的山门 容我再等 历史转身 等酒香醇 等你弹 一曲古筝" textColor:[selfrandomColor] labelFont:[UIFont systemFontOfSize:14]]];
[runLabel startAnimation];
}
- (UILabel *)createLabelWithText: (NSString *)text textColor:(UIColor *)textColor labelFont:(UIFont *)font {
NSString *string = [NSString stringWithFormat:@"%@", text];
CGFloat width = [UILabel getWidthByTitle:string font:font];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 50)];
label.font = font;
label.text = string;
label.textColor = textColor;
return label;
}
- (UIColor *)randomColor {
return [UIColor colorWithRed:[self randomValue] green:[self randomValue] blue:[self randomValue] alpha:1];
}
- (CGFloat)randomValue {
return arc4random()%255 / 255.0f;
}
总结
这个例子挺小的,主要思路就是利用动画将其变活,可能不太好理解的地方就是在动画移动的path这个路径的距离上,我们想这个路径的时候肯定要这样想,我让动画从最初我看不见的地方出现,然后最后到我看不见的地方消失,这个中间的距离之差就是屏幕的宽度了,而这个屏幕的宽度正好我们可以用contentView.frame来表示。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
# ios实现跑马灯效果
# ios
# 跑马灯label
# 文字跑马灯效果
# 学习iOS全局跑马灯
# iOS在固定的label上动态显示所有文字
# iOS使用UICountingLabel实现数字变化的动画效果
# iOS实现知乎和途家导航栏渐变的文字动画效果
# ios动态设置lbl文字标签的高度
# iOS 屏幕解锁文字动画效果
# iOS应用中UILabel文字显示效果的常用设置总结
# 跑马灯效果大全
# 基于Jquery的文字滚动跑马灯插件(一个页面多个滚动区)
# js跑马灯代码(自写)
# 自定义
# 移除
# 我是
# 遁入空门
# 来了
# 我看
# 也就
# 是怎么
# 可以用
# 设为
# 到我
# 是从
# 将其
# 这段
# 这两个
# 或者是
# 等你
# 来看看
# 可选
# 并对
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
EditPlus中的正则表达式实战(6)
js代码实现下拉菜单【推荐】
Laravel如何实现全文搜索功能?(Scout和Algolia示例)
Laravel的辅助函数有哪些_Laravel常用Helpers函数提高开发效率
详解vue.js组件化开发实践
合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?
javascript读取文本节点方法小结
如何撰写建站申请书?关键要点有哪些?
php中::能调用final静态方法吗_final修饰静态方法调用规则【解答】
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
详解阿里云nginx服务器多站点的配置
Bootstrap整体框架之JavaScript插件架构
公司网站制作价格怎么算,公司办个官网需要多少钱?
Android okhttputils现在进度显示实例代码
node.js报错:Cannot find module 'ejs'的解决办法
利用JavaScript实现拖拽改变元素大小
Swift中swift中的switch 语句
html如何与html链接_实现多个HTML页面互相链接【互相】
Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】
Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程
如何快速辨别茅台真假?关键步骤解析
如何在Tomcat中配置并部署网站项目?
原生JS实现图片轮播切换效果
如何在云主机上快速搭建多站点网站?
如何在Windows服务器上快速搭建网站?
HTML 中如何正确使用模板变量为元素的 name 属性赋值
如何在 React 中条件性地遍历数组并渲染元素
如何为不同团队 ID 动态生成多个独立按钮
企业网站制作这些问题要关注
历史网站制作软件,华为如何找回被删除的网站?
Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】
高防服务器租用首荐平台,企业级优惠套餐快速部署
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】
Midjourney怎样加参数调细节_Midjourney参数调整技巧【指南】
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
微信小程序 canvas开发实例及注意事项
软银砸40亿美元收购DigitalBridge 强化AI资料中心布局
如何做网站制作流程,*游戏网站怎么搭建?
Laravel如何处理异常和错误?(Handler示例)
怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?
Python文本处理实践_日志清洗解析【指导】
中山网站推广排名,中山信息港登录入口?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
logo在线制作免费网站在线制作好吗,DW网页制作时,如何在网页标题前加上logo?
Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】
教学论文网站制作软件有哪些,写论文用什么软件
?
UC浏览器如何设置启动页 UC浏览器启动页设置方法
Windows10电脑怎么设置虚拟光驱_Win10右键装载ISO镜像文件

