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 ...
随机推荐
- Oracle新建用户、角色,授权,建表空间
oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...
- 【转】Android 实现ListView的滑动删除效果
http://www.cnblogs.com/weixiao870428/p/3524055.html http://download.csdn.net/download/love_javc_you/ ...
- 关于DateTime.Now.Ticks
DataTime.Now.Ticks 的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔的间隔数,可用于较精确的计时. 1秒=1000豪秒 1毫秒=10 ...
- 权限检查联系人ProfileProvider
每日一贴,今天的内容关键字为权限检查 ProfileProvider继承自AbstractContyactsProvider. 源代码请自行下载 每日一道理 书籍好比一架梯子,它能引领人们登上 ...
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学
B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- Android内存优化解决 资料和总结的经验分享
在前公司做一个图片处理的应用时, 项目交付的时候,客户的手机在运行应用的时候,一直在崩溃,而这个异常就是OutOfMemory的错误,简称为OOM, 搞得我们也是极其的崩溃,最后 ,我们是通过网上搜集 ...
- SDWebImage内存性能优化
// 清除已过期图片 [[SDImageCache sharedImageCache] cleanDisk]; // 设置最多缓存7天时间 [[SDImageCache sharedImageCach ...
- [AngularJS] ngCloak
The ngCloak directive is used to prevent the Angular html template from being briefly displayed by t ...
- iOS开发——新特性篇&swift新特性(__nullable和__nonnull)
swift新特性(__nullable和__nonnull) 最近在看老师写代码的时候经常遇到两个陌生的关键字,但是当我在我的电脑上敲得时候就是敲不出,后来才知道这是为了swift与OC混编的时候产生 ...
- xcode 3.x版本中的Executables 到xcode 4.x中怎么找不到了?
转自:http://zhidao.baidu.com/question/327868169.html 1 在Scheme处选择Edit Scheme 2 点击Run(Debug) 3 在Argumen ...