/*
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位置

//從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é)束啦.^ ^;