使用UILabel实现滚动字幕移动效果
使用UILabel实现滚动字幕移动效果

这个链接中的代码也实现了这种效果
https://github.com/cbpowell/MarqueeLabel
最终效果如下:

原理如下:
1. 获取文本
2. 计算文本宽度
3. 将这个Label放入ScrollView中
4. 将ScrollView的contentSize的宽度设置与文本宽度一致
5. 做动画
*6. 边缘的渐隐效果请使用带透明像素的PNG图片

//
// RootViewController.m
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "YXKit.h"
#import "FontPool.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 注册字体
REGISTER_FONT(bundleFont(@"新蒂小丸子体.ttf"), @"新蒂小丸子体"); // 获取文本
NSString *string = @" 喜欢这首情思幽幽的曲子,仿佛多么遥远,在感叹着前世的情缘,又是那么柔软,在祈愿着来世的缠绵。《莲的心事》,你似琉璃一样的晶莹,柔柔地拨动我多情的心弦。我,莲的心事,有谁知?我,莲的矜持,又有谁懂? "; // 初始化label
UILabel *label = [UILabel new];
label.text = string;
label.numberOfLines = ;
label.textColor = [UIColor cyanColor];
label.font = [UIFont fontWithName:CUSTOM_FONT(@"新蒂小丸子体", )
size:.f]; // 计算尺寸
CGSize size = [label boundingRectWithSize:CGSizeMake(, )];
label.frame = (CGRect){CGPointZero, size}; // 初始化ScrollView
UIScrollView *showView = \
[[UIScrollView alloc] initWithFrame:CGRectMake(, , , size.height)];
showView.contentSize = size;
showView.showsHorizontalScrollIndicator = NO;
[showView addSubview:label];
[self.view addSubview:showView]; // 形成边缘的遮罩
UIImageView *imageView = \
[[UIImageView alloc] initWithFrame:CGRectMake(, , , size.height)];
imageView.image = [UIImage imageNamed:@"bg"];
[self.view addSubview:imageView]; // 动画
[UIView animateKeyframesWithDuration:
delay:
options:UIViewKeyframeAnimationOptionAllowUserInteraction
animations:^{
// 计算移动的距离
CGPoint point = showView.contentOffset;
point.x = size.width - .f;
showView.contentOffset = point;
}
completion:^(BOOL finished) { }];
} @end
使用UILabel实现滚动字幕移动效果的更多相关文章
- UILabel滚动字幕的实现
经常需要在应用中显示一段很长的文字,比如天气或者广告等,这时候使用滚动字幕的方式比较方便. 参考文献: [1] YouXianMing, 使用UILabel实现滚动字幕移动效果, 博客园 [2] ht ...
- js原生 + jQuery实现页面滚动字幕
js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 ...
- DS控件库 DSLed控件呈现滚动字幕效果
滚动字幕效果在DSled上可以使用偏移来实现,代码如下 运行效果
- HTML滚动字幕代码参数详解及Js间隔滚动代码
html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...
- C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans
Lable的Left属性是能够更改的,可是 Right属性不能够更改,所以我们能够利用 这个特点做自加 自减运算 using System; using System.Collections.Gene ...
- 【Cocos2dx 3.3 Lua】滚动字幕
参考资料: http://blog.csdn.net/jackystudio/article/details/12991977 1.原理 通过调用update来更新位置达到 ...
- Qt之滚动字幕
简述 滚动字幕,也就是传说中的跑马灯效果. 简单地理解就是:每隔一段时间(一般几百毫秒效果较佳)显示的文字进行变化(即滚动效果). 简述 实现 效果 源码 实现 利用定时器QTimer,在固定的时间 ...
- python 滚动字幕
写在前面:最近学python,爬虫方面感兴趣,顺便还可以了解下人工智能吧. 下面是两种方式做滚动字幕,直接贴代码了: 1.第一种: import time advText = input(" ...
- JavaCV 视频滤镜(LOGO、滚动字幕、画中画、NxN宫格)
其实,在JavaCV中除了FFmpegFrameGrabber和FFmpegFrameRecorder之外,还有一个重要的类,那就是FFmpegFrameFilter. FFmpegFrameFilt ...
随机推荐
- hibernate核心开发接口_Configuration
AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...
- rpm: error while loading shared libraries: libgcc_s.so.1: cannot open shared object file: No such file or directory解决办法
不多说,直接上干货! 问题详情 [root@bigdatamaster app]# rpm -qa | grep gcc rpm: error : cannot open shared object ...
- Ubuntu 16.04 RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller” 不能上网
来源:http://forum.ubuntu.org.cn/viewtopic.php?f=116&t=463646 1.执行如下命令 uname -a sudo lspci -knn sud ...
- JavaScript数据结构-13.散列碰撞(开链法)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 安装Ubunutu音频视频库
sudo apt-get install ubuntu-restricted-extras
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
- VSCode基本配置
功能:保存时自动按ESLint规范格式化代码 + stylus配置(需安装Manta's Stylus Supremacy插件) { "editor.fontSize": 18, ...
- linux ssh 免密码登录的配置过程
# ssh-keygen -t rsa -C "自定义描述" -f ~/.ssh/自定义生成的rsa文件 # cd ./.ssh # touch config # 粘贴 Host ...
- developer.android.google.cn
Android Studio官方 Android IDE https://developer.android.google.cn/studio/index.html 探索 Android Studio ...
- [转]Calling an OData Service From a .NET Client (C#)
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata- ...