iOS 字体滚动效果 ScrollLabel
写了一个简单的字体滚动效果。
用了一种取巧的方式,传入两个一摸一样的Label(当然也可以是别的视图), 话不多说,代码里面讲解。
SEScrollLabel.h
#import <UIKit/UIKit.h>
/*! @brief 回调代码块
*
* 当滚动效果持续loopsDone次之后,isFinished值会变为YES,执行代码块
* @param loopsDone 滚动效果执行次数
* @param isFinished 是否已经结束
*/
typedef void (^PMAnimationFinished)(NSUInteger loopsDone, BOOL isFinished);
enum {
PMScrollDirectionFromLeft = 0, // Animation starts from left and continues to the right side
PMScrollDirectionFromRight = 1 // Animation starts from right and continues back to the left side
}; typedef NSUInteger PMScrollDirection;
@interface SEScrollLabel : UIScrollView
/*!
*
* @param frame 滚动视图的frame
* @param labels 存放UILabel数组,这个demo目前最多支持两个
* @param scrollDirection 滚动方向
* @param scrollSpeed 滚动速度,单位是 CGFloat/s
* @param loops 滚动执行次数
* @param completition 执行结束调用方法
* @return 返回SEScrollLabel对象
*/
-(instancetype)initWithFrame:(CGRect) frame labels:(NSArray *)labels
direction:(PMScrollDirection) scrollDirection
speed:(CGFloat) scrollSpeed
loops:(NSUInteger) loops
completition:(PMAnimationFinished) completition;
-(void)setText:(NSString *)text;
- (void) beginAnimation;
-(void)resetAnimation;
@end
SEScrollLabel.m
#import "SEScrollLabel.h"
#define LABEL_SPACING 30 //定义了字体滚动的间隙
@interface SEScrollLabel()
@property (nonatomic, assign) BOOL needAnimating; //是否需要滚动,label长度大于scrollLabel长度,设为YES,否则为NO
@property (nonatomic, assign) BOOL isAnimating; //是否正在滚动
@property (nonatomic, assign) CGFloat scrollSpeed; //滚动速度
@property (nonatomic, assign) NSInteger numberOfLoops; //滚动循环次数
@property (nonatomic, assign) NSInteger runningLoops; //当前已经执行过的滚动次数
@property (nonatomic, assign) PMScrollDirection scrollDirection; //滚动方向
@property (nonatomic, strong) PMAnimationFinished finishedBlock; //回调代码块
@end
@implementation SEScrollLabel
-(instancetype)initWithFrame:(CGRect) frame labels:(NSArray *)labels
direction:(PMScrollDirection) scrollDirection
speed:(CGFloat) scrollSpeed
loops:(NSUInteger) loops
completition:(PMAnimationFinished) completition
{
self = [super initWithFrame:frame];
if (self != nil)
{
UILabel *label = [labels firstObject];
CGRect labelFrame = label.frame;
if (label.frame.size.width <= frame.size.width) //如果第一个label宽度小于视图宽度,则判断不需要滚动显示
{
self.contentSize = CGSizeMake(labelFrame.size.width , labelFrame.size.height);
labelFrame.origin.x = frame.size.width - labelFrame.size.width;
labelFrame.origin.y = 0;
label.frame = labelFrame;
[self addSubview:label];
_needAnimating = NO;
}
else
{
self.contentSize = CGSizeMake(labelFrame.size.width *2 + LABEL_SPACING *2 , labelFrame.size.height);
labelFrame.origin.x = 0;
labelFrame.origin.y = 0;
label.frame = labelFrame;
[self addSubview:label];
if (scrollDirection == PMScrollDirectionFromRight)
{
labelFrame.origin.x += LABEL_SPACING + labelFrame.size.width;
}
else if (scrollDirection == PMScrollDirectionFromLeft)
{
labelFrame.origin.x -= LABEL_SPACING + labelFrame.size.width;
}
UILabel *sameLabel = [labels lastObject];
sameLabel.frame = labelFrame;
[self addSubview:sameLabel];
self.scrollDirection = scrollDirection;
self.scrollSpeed = scrollSpeed;
self.numberOfLoops = loops;
self.finishedBlock = completition;
self.needAnimating = YES;
}
}
return self;
}
- (void) beginAnimation
{
//所做的动画非常简单,就是让scrollView的conentOffset从0开始到contentSize.width/2的距离内不断循环,这里需要注意contentSize是怎么设置才能达到想要的效果
if (!self.needAnimating) return;
if (self.isAnimating) return;
[self setContentOffset:CGPointZero];
self.isAnimating = YES;
NSTimeInterval animationDuration = (self.contentSize.width/self.scrollSpeed);
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGPoint finalPoint = CGPointZero;
if (self.scrollDirection == PMScrollDirectionFromRight)
finalPoint = CGPointMake(self.contentSize.width/2, 0);
else if (self.scrollDirection == PMScrollDirectionFromLeft)
finalPoint = CGPointMake(-self.contentSize.width/2, 0);
self.contentOffset = finalPoint;
} completion:^(BOOL finished) {
if (finished) {
self.isAnimating = NO;
BOOL restartAnimation = (self.numberOfLoops == 0 || self.runningLoops <= self.numberOfLoops);
if (self.finishedBlock)
{
self.finishedBlock((self.runningLoops+1),!restartAnimation);
}
if (restartAnimation)
[self beginAnimation];
else
[self endAnimation:NO];
self.runningLoops++;
}
}];
}
-(void)resetAnimation
{
//重置循环,将计数变为0,然后重新开始滚动
self.isAnimating = NO;
self.runningLoops = 0;
[self beginAnimation];
}
- (void) endAnimation:(BOOL) animated {
if (!self.isAnimating) return;
self.isAnimating = NO;
[self setContentOffset:CGPointZero animated:NO];
}
-(void)setText:(NSString *)text
{
//改变文字,这里我偷了个懒,没有重新去修改contentSize和label的frame, 所以运行效果可能有些问题,根据自己想要的效果进行修改吧
for (UIView *view in self.subviews)
{
if ([view isKindOfClass:[UILabel class]])
{
[(UILabel *)view setText:text];
}
}
}
@end
用法实例:
SEScrollLabel *scrollLabel = [[SEScrollLabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30) labels:@[label, sameLabel] direction:PMScrollDirectionFromRight speed:15 loops:3 completition:^(NSUInteger loopsDone, BOOL isFinished) {
NSLog(@"已经运行了%ld次,循环%@结束", (unsigned long)loopsDone, isFinished ? @"已经": @"尚未");
}];
[self.view addSubview:scrollLabel];
[scrollLabel beginAnimation];
附上效果图一枚:

具体源码请访问 http://www.cnblogs.com/sely-ios/p/4552134.html
iOS 字体滚动效果 ScrollLabel的更多相关文章
- IOS UIScrollView + UIButton 实现segemet页面和顶部标签页水平滚动效果
很长一段时间没有写博客了,最近在学习iOS开发,看了不少的代码,自己用UIScrollView和UIButton实现了水平滚动的效果,有点类似于今日头条的主界面框架,效果如下: 代码如下: MyScr ...
- IOS设备上网页中的页面滚动效果模拟
可能咋一看不知道我说的是个啥,因为iOS本来就用这功能的啊,还模拟它干啥?先听我说下项目背景哈 我现在开发的是一个webapp,主要是用在ipad上,这个app的大小是固定大小的,为了防止触摸它出现弹 ...
- iOS UIWebView 获取内容实际高度,关闭滚动效果
本文转载至 http://my.oschina.net/Khiyuan/blog/341535 iOS UIWebView 获取内容实际高度,关闭滚动效果 近期做东西,将 UIWebView 嵌套 ...
- [ios]新手笔记-。-UIPickerView 关于伪造循环效果和延时滚动效果
查找了网上资料,循环效果绝大部分都是增加行数来制造循环的错觉,延时滚动就是利用NSTimer间隔出发滚动事件来制造滚动效果. 代码: #import <UIKit/UIKit.h>#imp ...
- [Swift通天遁地]九、拔剑吧-(13)创建页面的景深视差滚动效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]九、拔剑吧-(14)创建更美观的景深视差滚动效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 全屏滚动效果H5FullscreenPage.js
前提: 介于现在很多活动都使用了 类似全屏滚动效果 尤其在微信里面 我自己开发了一个快速构建 此类项目的控件 与市面上大部分控件不同的是此控件还支持元素的动画效果 并提供多种元素效果 基于zepto. ...
- marquee 实现首尾相连循环滚动效果
<marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标签不仅可以滚动文字,也可以滚动图片,表格等 marquee标签不是HTML3.2 ...
- fullPage教程 -- 整屏滚动效果插件 fullpage详解
1.引用文件 [html] view plain copy print?在CODE上查看代码片派生到我的代码片 <link rel="stylesheet" href=&qu ...
随机推荐
- 【不积跬步,无以致千里】安装roundcube 时出现 “DSN (write): NOT OK(SQLSTATE[HY000] [2002] No such file or directory)”
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- UVa839 Not so Mobile
我的解法: 建树,递归判断 #include<cstdio> #include<cstring> #include<iostream> #include< ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- delphi queryCommandState
如何 获取当前光标所在的字符属性 关键点 function queryCommandState(const cmdID: WideString): WordBool; safecall; 粗体 斜体 ...
- 如何通过VIM把代码格式化后生成HTML网页代码
本文转自http://wangxiaoyu.blog.51cto.com/922065/203471 需求及思路:演示需要,需要网站上嵌入一些代码,我的建议做法是根据代码文件,生成相应的HTML代码, ...
- 三大文本处理工具grep、sed及awk的简单介绍
grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...
- requestFileSystem is undefined
使用phonegap 3时,用File API,不应该用cordova plugin add org.apache.cordova.file等添加File API Plugin. 要用: phoneg ...
- Yar - Yet Another RPC framework for PHP
Introduction Yar is a RPC framework which aims to provide a simple and easy way to do communication ...
- IP101A芯片默认物理地址(PHY Adress)确定
转载:http://blog.csdn.net/ropai/article/details/6961157 根据IP101A的DataSheet,芯片的第9,10,12,13,15脚为PHYAD0~P ...
- PHP.10-PHP实例(一)-简单的计算器
PHP-简单的计算器 [PHP语法详解] PHP在web开发中的应用 PHP编写步骤 1.编写一个后缀名为.php文件2.上传到Web服务器的文档根目录下3.通过浏览器访问Web服务器管理下的PHP文 ...