广播(跑马灯)的实现
2016-01-23 本文已影响4492人
一起去买菜
/*
1.修改frame
实现思路:
CAKeyframeAnimation
重点: 给主view.layer添加keyframeAnimation动画
Snip20160123_1.png
*/
实现效果:
Untitled.gif
1.gif
1.首先自定义一个view继承自view用来盛放子控件
@interface ZHXLabel : UIView
/** <#name#> */
//喇叭图片
@property (strong, nonatomic)UIImage *image;
//文字
@property (nonatomic, strong) NSString *text;
//动画播放速度:使用枚举
@property (nonatomic, assign) ZHXSpeed speed;
// 循环滚动次数(为0时无限滚动)
@property (nonatomic, assign) NSUInteger repeatCount;
//占位控件
@property (nonatomic, strong) UIView *viewGap;
//添加子控件方法
- (void)reloadView;
@end
2..m中的实现
//用代码初始化
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.font = [UIFont systemFontOfSize:15];
self.speed = ZHXSpeedMild;
self.repeatCount = 0;
//超出主view部分剪切.
self.clipsToBounds = YES;
[self setup];
}
return self;
}
主要实现方法
//创建view
- (void)reloadView
{
//删除内部view.layer的动画
[self.innerContainer.layer removeAnimationForKey:@"move"];
CGFloat width1 = 0.f;
NSDictionary *attr = @{NSFontAttributeName : self.font};
// 新方法 : boundingRectWithSize 计算文本bounds (CGFLOAT_MAX 宽度范围是不确定的所以要用MAX.)这个方法返回任意NSString的size.widht.
CGSize size = [self.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.bounds.size.height) options:0 attributes:attr context:nil].size;
width1 = size.width;
CGFloat width = width1;
//如果文本width大于view的width 就移动
moveNeed = width > self.bounds.size.width;
//设置label的fram
CGRect f = CGRectMake(0, 0, width, self.bounds.size.height);
/*---------------------------*/
//创建label-1
UILabel *label = [[UILabel alloc] initWithFrame:f];
label.backgroundColor = [UIColor whiteColor];
//设置label文字.
label.text = self.text;
//将label添加到内部view空间中.
[self.innerContainer addSubview:label];
/*---------------------------*/
//如果需要移动.
if (moveNeed) {
/*---------------------------*/
//创建间隔view
self.viewGap = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame), 0, 50, self.bounds.size.height)];
self.viewGap.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.viewGap];
//viewGap 文字段落间隔. 创建下一个label 用来循环移动.
CGRect f1 = CGRectMake(width + self.viewGap.bounds.size.width, 0, width, f.size.height);
/*---------------------------*/
//创建label-2
UILabel *next = [[UILabel alloc] initWithFrame:f1];
self.backgroundColor = [UIColor whiteColor];
next.backgroundColor = [UIColor whiteColor];
if (self.text) {
next.text = self.text;
next.textColor = self.textColor;
next.font = self.font;
} else {
next.attributedText = self.attributedText;
}
[self.innerContainer addSubview:next];
//动画 - > 改变x
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
//关键帧.动画对象在指定时间内依次执行关键帧.
//具体就是 动画从0这个位置 执行到 x位置
![Snip20160123_3.png](http:https://img.haomeiwen.com/i1240886/2d910edbbd4c91e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
//从x=0开始,执行到x=0(需求:停顿一下) 再从x = 0 执行到最后.
moveAnimation.values = @[@0,@0 ,@( - width - self.viewGap.bounds.size.width)];
//keyTimes对应执行上面的values属性,x = 0到x = 0 执行了总时间的0.1(因为keyTimes与values是一一对应的)达到停顿效果,x = 0 到最后可以自定义.
moveAnimation.keyTimes = @[@0, @0.100, @0.868, @1.0];
//动画持续总时间. rate是自定义的.width相当于主空间的view
moveAnimation.duration = width / rate;
moveAnimation.repeatCount = self.repeatCount == 0 ? INT16_MAX : self.repeatCount;
[self.innerContainer.layer addAnimation:moveAnimation forKey:@"move"];
}
}
动画部分详解:
Snip20160123_3.png
愉快的一天又要结束啦.^ ^;