iOS 实现跑马灯效果的方法示例

所属分类: 软件编程 / IOS 阅读数: 758
收藏 0 赞 0 分享

在网页开发当中跑马灯是常用到的,用来显示通知等,在游戏开发当中也如此。

首先来看看效果图:

接下来就简单看看这效果是怎么实现的。

实现方法

1、首先我们从这个图片里面能联想到如果实现这个效果必然需要使用到动画,或者还有有用scrollView的思路,这里我是用的动画的方式实现的。

2、.h文件

自定义一个继承UIView的LGJAutoRunLabel类,在.h文件中:

@class LGJAutoRunLabel;

typedef NS_ENUM(NSInteger, RunDirectionType) {
 LeftType = 0,
 RightType = 1,
};

@protocol LGJAutoRunLabelDelegate <NSObject>

@optional
- (void)operateLabel: (LGJAutoRunLabel *)autoLabel animationDidStopFinished: (BOOL)finished;

@end

@interface LGJAutoRunLabel : UIView

@property (nonatomic, weak) id <LGJAutoRunLabelDelegate> delegate;
@property (nonatomic, assign) CGFloat speed;
@property (nonatomic, assign) RunDirectionType directionType;

- (void)addContentView: (UIView *)view;
- (void)startAnimation;
- (void)stopAnimation;

定义一个NS_ENUM用来判断自动滚动的方向,分别是左和右,声明一个可选类型的协议,用来在controller中调用并对autoLabel进行操作。声明对外的属性和方法。这里一目了然,主要的实现思路都集中在.m文件中。

3、.m文件

声明“私有”变量和属性:

@interface LGJAutoRunLabel()<CAAnimationDelegate>
{
 CGFloat _width;
 CGFloat _height;
 CGFloat _animationViewWidth;
 CGFloat _animationViewHeight;
 BOOL _stoped;
 UIView *_contentView;//滚动内容视图
}
@property (nonatomic, strong) UIView *animationView;//放置滚动内容视图
@end

初始化方法:

- (instancetype)initWithFrame:(CGRect)frame {

 if (self == [super initWithFrame:frame]) {
 _width = frame.size.width;
 _height = frame.size.height;

 self.speed = 1.0f;
 self.directionType = LeftType;
 self.layer.masksToBounds = YES;
 self.animationView = [[UIView alloc] initWithFrame:CGRectMake(_width, 0, _width, _height)];
 [self addSubview:self.animationView];
 }
 return self;
}

将滚动内容视图contentView添加到动画视图animationView上:

- (void)addContentView:(UIView *)view {

 [_contentView removeFromSuperview];
 view.frame = view.bounds;
 _contentView = view;
 self.animationView.frame = view.bounds;
 [self.animationView addSubview:_contentView];

 _animationViewWidth = self.animationView.frame.size.width;
 _animationViewHeight = self.animationView.frame.size.height;
}

让animationView上的contentView自动滚动起来的主要方法在这儿,重点来了,就是这个- (void)startAnimation方法,看一下这个方法里面是怎么样实现的:

- (void)startAnimation {
 [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
 _stoped = NO;

 CGPoint pointRightCenter = CGPointMake(_width + _animationViewWidth / 2.f, _animationViewHeight / 2.f);
 CGPoint pointLeftCenter = CGPointMake(-_animationViewWidth / 2, _animationViewHeight / 2.f);
 CGPoint fromPoint = self.directionType == LeftType ? pointRightCenter : pointLeftCenter;
 CGPoint toPoint  = self.directionType == LeftType ? pointLeftCenter : pointRightCenter;

 self.animationView.center = fromPoint;
 UIBezierPath *movePath = [UIBezierPath bezierPath];
 [movePath moveToPoint:fromPoint];
 [movePath addLineToPoint:toPoint];

 CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
 moveAnimation.path   = movePath.CGPath;
 moveAnimation.removedOnCompletion = YES;
 moveAnimation.duration  = _animationViewWidth / 30.f * (1 / self.speed);
 moveAnimation.delegate  = self;
 [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];
}

↘️首先先把animationView.layer上的动画移除掉,接下来就是要找到animationView\contentView的pointCenter这里把这个中点当做是animationView或者是contentView都行,因为这两个视图的frame是相等的,这步找左右中点的意义在于,完全显示出文字内容,因为可能会遇到那种比如文字太长了,view长度不够,不能完全显示出来文字的全部内容, 这里我们找到中点,也就相当于确定了内容视图要滑动的范围了,接下来根据起始方向的枚举值设置fromPoint和toPoint这里我们默认是从右向左滚动的。这里我们做动画设置,用到了贝塞尔曲线,我们设置UIBezierPath的起始位置就是fromPoint也就是屏幕右方(我们看不见)self.animationView.center。终止位置是屏幕左方toPoint此时animationView滚动的起始位置的首和终止位置的尾的距离正好是屏幕的宽度。这里我们使用CAKeyframeAnimation关键帧动画,moveAnimation.path                 = movePath.CGPath; ,moveAnimation.removedOnCompletion  = YES;动画完成后就将动画移除,不保留最终的状态。 [self.animationView.layer addAnimation:moveAnimation forKey:@"animationViewPosition"];将动画添加到animationView.layer上。

(这段是对上面代码的解释)

-------------------分割线-------------------

接下来的这个就是代理方法的实现了,当动画完成后悔调用LGJAutoRunLabelDelegate我们自定义的delegate方法。

- (void)stopAnimation {
 _stoped = YES;
 [self.animationView.layer removeAnimationForKey:@"animationViewPosition"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
 if (self.delegate && [self.delegate respondsToSelector:@selector(operateLabel:animationDidStopFinished:)]) {
 [self.delegate operateLabel:self animationDidStopFinished:flag];
 }
 if (flag && !_stoped) {
 [self startAnimation];
 }
}

4、在controller中使用方法

主要的方法就是声明LGJAutoRunLabel实例,将代理设为自身,声明directionType默认为Left,在runLabel上创建label也就是我们看到的文字。其余方法一目了然了。

//MARK:- CreateAutoRunLabel
- (void)createAutoRunLabel {
 LGJAutoRunLabel *runLabel = [[LGJAutoRunLabel alloc] initWithFrame:CGRectMake(0, 100, kWidth, 50)];
 runLabel.delegate = self;
 runLabel.directionType = LeftType;
 [self.view addSubview:runLabel];
 [runLabel addContentView:[self createLabelWithText:@"繁华声 遁入空门 折煞了梦偏冷 辗转一生 情债又几 如你默认 生死枯等 枯等一圈 又一圈的 浮图塔 断了几层 断了谁的痛直奔 一盏残灯 倾塌的山门 容我再等 历史转身 等酒香醇 等你弹 一曲古筝" textColor:[selfrandomColor] labelFont:[UIFont systemFontOfSize:14]]];
 [runLabel startAnimation];
}

- (UILabel *)createLabelWithText: (NSString *)text textColor:(UIColor *)textColor labelFont:(UIFont *)font {
 NSString *string = [NSString stringWithFormat:@"%@", text];
 CGFloat width = [UILabel getWidthByTitle:string font:font];
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 50)];
 label.font = font;
 label.text = string;
 label.textColor = textColor;
 return label;
}

- (UIColor *)randomColor {
 return [UIColor colorWithRed:[self randomValue] green:[self randomValue] blue:[self randomValue] alpha:1];
}

- (CGFloat)randomValue {
 return arc4random()%255 / 255.0f;
}

这个例子挺小的,主要思路就是利用动画将其变活,可能不太好理解的地方就是在动画移动的path这个路径的距离上,我们想这个路径的时候肯定要这样想,我让动画从最初我看不见的地方出现,然后最后到我看不见的地方消失,这个中间的距离之差就是屏幕的宽度了,而这个屏幕的宽度正好我们可以用contentView.frame来表示。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

更多精彩内容其他人还在看

iOS开发笔记--详解UILabel的相关属性设置

这篇文章主要介绍了iOS开发笔记--详解UILabel的相关属性设置,对初学者具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

iOS中获取系统相册中的图片实例

这篇文章主要介绍了iOS中获取系统相册中的图片实例,具有一定的参考价值没有需要的朋友可以了解一下。
收藏 0 赞 0 分享

iOS 检测网络状态的两种方法

一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

IOS 性能优化中离屏渲染

本文主要介绍了IOS 性能优化中离屏渲染的资料,提供了几种方法讲解了优化,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

这篇文章主要介绍了iOS获取当前设备型号设备信息的总结包含iPhone7和iPhone7P,包括ios7之前之后的获取方式,本文接的非常详细,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现爆炸的粒子效果示例代码

之前在网上看到了一个Android实现的爆炸效果,感觉非常不错,所以自己尝试用iOS来实现下效果,现在将实现的过程、原理以及遇到的问题分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

iOS毕业设计之天气预报App

这篇文章主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS轻点、触摸和手势代码开发

这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 实现多代理的方法及实例代码

这篇文章主要介绍了iOS 实现多代理的方法及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS文字渐变色效果的实现方法

在大家日常开发iOS的过程中,可能会遇到要实现文字渐变色的效果,这篇文章文章通过示例代码和详细的步骤介绍了如何利用iOS实现文字渐变色的效果,实现后的很不错,感兴趣的朋友们下面来一起看看吧。
收藏 0 赞 0 分享
查看更多