广播(跑马灯)的实现

/*

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://upload-images.jianshu.io/upload_images/1240886-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

愉快的一天又要结束啦.^ ^;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,164评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,613评论 6 30
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,644评论 25 709
  • 【蜗牛计划,每天进步一点点】 我是清泉 打卡日期:2017年8月6日 打卡天数:第37天 (1)我今年的三个年度目...
    沈曼柔阅读 158评论 0 0
  • 2016年5月,第一次接触电商,到安馨生物堂总公司参观学习,加入了电商大军,退伍后的第一份事业,定了方向,那就准备...
    微校友团队阅读 208评论 0 0