廣播(跑馬燈)的實(shí)現(xiàn)

/*

1.修改frame
實(shí)現(xiàn)思路:

CAKeyframeAnimation
重點(diǎn): 給主view.layer添加keyframeAnimation動(dòng)畫


Snip20160123_1.png

*/
實(shí)現(xiàn)效果:


Untitled.gif

1.gif

1.首先自定義一個(gè)view繼承自view用來(lái)盛放子控件

@interface ZHXLabel : UIView

/** <#name#> */
//喇叭圖片
@property (strong, nonatomic)UIImage *image;
//文字
@property (nonatomic, strong) NSString *text;
//動(dòng)畫播放速度:使用枚舉
@property (nonatomic, assign) ZHXSpeed speed;
// 循環(huán)滾動(dòng)次數(shù)(為0時(shí)無(wú)限滾動(dòng))
@property (nonatomic, assign) NSUInteger repeatCount;
//占位控件
@property (nonatomic, strong) UIView *viewGap;
//添加子控件方法
- (void)reloadView;
@end

2..m中的實(shí)現(xiàn)

//用代碼初始化
- (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;
}

主要實(shí)現(xiàn)方法

//創(chuàng)建view
- (void)reloadView
{
    //刪除內(nèi)部view.layer的動(dòng)畫
    [self.innerContainer.layer removeAnimationForKey:@"move"];
  
    CGFloat width1 = 0.f;
    NSDictionary *attr = @{NSFontAttributeName : self.font};
    // 新方法 : boundingRectWithSize 計(jì)算文本bounds (CGFLOAT_MAX 寬度范圍是不確定的所以要用MAX.)這個(gè)方法返回任意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 就移動(dòng)
    moveNeed = width > self.bounds.size.width;
  
    //設(shè)置label的fram
    CGRect f = CGRectMake(0, 0, width, self.bounds.size.height);

    /*---------------------------*/
    //創(chuàng)建label-1
    UILabel *label = [[UILabel alloc] initWithFrame:f];
    label.backgroundColor = [UIColor whiteColor];
        //設(shè)置label文字.
        label.text = self.text;
    //將label添加到內(nèi)部view空間中.
    [self.innerContainer addSubview:label];
    /*---------------------------*/
    //如果需要移動(dòng).
    if (moveNeed) {
      
    /*---------------------------*/
        //創(chuàng)建間隔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 文字段落間隔. 創(chuàng)建下一個(gè)label 用來(lái)循環(huán)移動(dòng).
        CGRect f1 = CGRectMake(width + self.viewGap.bounds.size.width, 0, width, f.size.height);
    /*---------------------------*/
//創(chuàng)建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];
        
        //動(dòng)畫 - > 改變x
        CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
        
        //關(guān)鍵幀.動(dòng)畫對(duì)象在指定時(shí)間內(nèi)依次執(zhí)行關(guān)鍵幀.
//具體就是 動(dòng)畫從0這個(gè)位置 執(zhí)行到 x位置

![Snip20160123_3.png](http://upload-images.jianshu.io/upload_images/1240886-2d910edbbd4c91e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
//從x=0開始,執(zhí)行到x=0(需求:停頓一下) 再?gòu)膞 = 0 執(zhí)行到最后.
        moveAnimation.values = @[@0,@0 ,@( - width - self.viewGap.bounds.size.width)];
//keyTimes對(duì)應(yīng)執(zhí)行上面的values屬性,x = 0到x = 0 執(zhí)行了總時(shí)間的0.1(因?yàn)閗eyTimes與values是一一對(duì)應(yīng)的)達(dá)到停頓效果,x = 0 到最后可以自定義.
        moveAnimation.keyTimes = @[@0, @0.100, @0.868, @1.0];

        //動(dòng)畫持續(xù)總時(shí)間. rate是自定義的.width相當(dāng)于主空間的view
        moveAnimation.duration = width / rate;
       
        moveAnimation.repeatCount = self.repeatCount == 0 ? INT16_MAX : self.repeatCount;

        
        [self.innerContainer.layer addAnimation:moveAnimation forKey:@"move"];
    }
}

動(dòng)畫部分詳解:


Snip20160123_3.png

愉快的一天又要結(jié)束啦.^ ^;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,270評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,696評(píng)論 6 30
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,150評(píng)論 25 708
  • 【蝸牛計(jì)劃,每天進(jìn)步一點(diǎn)點(diǎn)】 我是清泉 打卡日期:2017年8月6日 打卡天數(shù):第37天 (1)我今年的三個(gè)年度目...
    沈曼柔閱讀 179評(píng)論 0 0
  • 2016年5月,第一次接觸電商,到安馨生物堂總公司參觀學(xué)習(xí),加入了電商大軍,退伍后的第一份事業(yè),定了方向,那就準(zhǔn)備...
    微校友團(tuán)隊(duì)閱讀 225評(píng)論 0 0

友情鏈接更多精彩內(nèi)容