在有些廣播電臺或者是音樂中,不需要或者不想用音樂光盤轉(zhuǎn)動的時候可以用到音樂震動條,來讓畫面變得不單調(diào).
//創(chuàng)建復(fù)制層
CAReplicatorLayer*relayer = [CAReplicatorLayerlayer];
//設(shè)置位置為控制器view的中心點
relayer.position=self.view.center;
//設(shè)置復(fù)制層的大小
relayer.bounds=CGRectMake(0,0,250,250);
//設(shè)置背景顏色
relayer.backgroundColor= [UIColorlightGrayColor].CGColor;
//將復(fù)制層添加到控制器的layer上
[self.view.layeraddSublayer:relayer];
//創(chuàng)建震動條
CALayer*subLayer = [[CALayeralloc]init];
//設(shè)置背景顏色
subLayer.backgroundColor= [UIColoryellowColor].CGColor;
//設(shè)置寬高.
subLayer.bounds=CGRectMake(0,0,30,150);
//設(shè)置震動層的位置
subLayer.position=CGPointMake(40,200);
//給定震動層的錨點
subLayer.anchorPoint=CGPointMake(0.5,1);
//將需要被賦值的震動層添加到復(fù)制層
[relayeraddSublayer:subLayer];
//創(chuàng)建關(guān)鍵幀動畫
CABasicAnimation*animation = [CABasicAnimationanimationWithKeyPath:@"transform.scale.y"];
//結(jié)束值
animation.toValue=@0.1;
//動畫結(jié)束時是否執(zhí)行逆動畫
animation.autoreverses=YES;
//重復(fù)次數(shù)(無限)
animation.repeatCount=MAXFLOAT;
//添加到震動層
[subLayeraddAnimation:animationforKey:nil];
//設(shè)置復(fù)制層需要賦值的個數(shù)
relayer.instanceCount=5;
//設(shè)置賦值出來的子層的位置
relayer.instanceTransform=CATransform3DMakeTranslation(40,0,0);
//執(zhí)行動畫的延遲
relayer.instanceDelay=0.2;
//顏色偏移量范圍(0-1)
relayer.instanceGreenOffset= -0.8;