写了一个简单的字体滚动效果。
用了一种取巧的方式,传入两个一摸一样的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];
附上效果图一枚: