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 ...
随机推荐
- zookeeper 系列
ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...
- Klist
显示当前缓存的 Kerberos 票证的列表. 有关如何使用此命令的示例 语法 klist [-<LogonId.HighPart> lh] [-li <LogonId.LowPar ...
- 方法字段[C# 基础知识系列]专题二:委托的本质论
首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 引言: 上一个专题已和大家分享了我懂得的——C#中为什么须要委托,专题中简略介绍了下委托是什么以及委托简略的应用的,在这个专题中将对委托做进 ...
- iOS开发技巧系列---详解KVC(我告诉你KVC的一切)
KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...
- cocos2dx 3.1从零学习(一)——入门篇(一天学会打飞机)
没办法,浏览这么高,为啥没人投票呢?朋友们,我这篇文章參加了csdn博文大赛.喜欢的来点个赞吧!点击:http://vote.blog.csdn.net/Article/Details?article ...
- 转:打造DropDownList,TreeView,ListBox无限极分类目录树
[csharp] view plaincopyprint? #region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// ...
- C#_自动化测试1_模拟post,get_12306火车票网站自动登录工具
还记得2011年春运,12306火车票预订网站经常崩溃无法登录吗. 今天我们就开发一个12306网站自动登录软件. 帮助您轻松订票 通过前两篇博客Fiddler教程和HTTP协议详解,我们了解了Web ...
- C#_判断2个对象的值是否相等
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 网卡及MAC和PHY的区别
转载:http://blog.sina.com.cn/s/blog_53d7350f0100mudb.html 一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数 ...
- App安全之网络传输安全
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...