iOS 動(dòng)畫效果實(shí)現(xiàn)隨記


/// 開始旋轉(zhuǎn)動(dòng)畫,效果:Y軸360度旋轉(zhuǎn)后停留2秒再繼續(xù)旋轉(zhuǎn)

- (void) startRotationAnimation {

? ? // 添加旋轉(zhuǎn)動(dòng)畫

? ? CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];

? ? rotationAnimation.fromValue= [NSNumber numberWithFloat:0];

? ? rotationAnimation.toValue= [NSNumber numberWithFloat:M_PI*2];

? ? rotationAnimation.duration=1.5;// 旋轉(zhuǎn)動(dòng)畫1.5秒完成

? ? rotationAnimation.repeatCount=1;


? ? // 創(chuàng)建動(dòng)畫組

? ? CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

? ? animationGroup.animations=@[rotationAnimation];

? ? animationGroup.duration = 3.5; //間隔2秒后再次開始旋轉(zhuǎn)動(dòng)畫

? ? animationGroup.repeatCount=HUGE_VALF;

? ? [self.layer removeAnimationForKey:@"RotationAnimation"];

? ? [self.layer addAnimation:animationGroup forKey:@"RotationAnimation"];

}


/// 縮放動(dòng)畫,效果:1-1.2倍循環(huán)縮放

- (void) startScaleAnimation {

? ? // 縮放動(dòng)畫

? ? CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

? ? scaleAnimation.fromValue= [NSNumber numberWithFloat:1.0];

? ? scaleAnimation.toValue= [NSNumber numberWithFloat:1.2];

? ? scaleAnimation.duration=0.4;

? ? scaleAnimation.autoreverses=YES;

? ? scaleAnimation.repeatCount=HUGE_VALF;


? ? // 創(chuàng)建動(dòng)畫組

? ? CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

? ? animationGroup.animations=@[scaleAnimation];

? ? animationGroup.duration=0.8;

? ? animationGroup.repeatCount=HUGE_VALF;


? ? [self.layer removeAnimationForKey:@"ScaleAnimation"];

? ? [self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];

}


/// 晃動(dòng)效果動(dòng)畫

- (void) startShakeAnimation{

? ? // 晃動(dòng)動(dòng)畫

? ? CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];

? ? shakeAnimation.values=@[@(-0.3),@(0.3),@(-0.3)];// 晃動(dòng)角度

? ? shakeAnimation.keyTimes=@[@(0),@(0.5),@(1)];// 晃動(dòng)時(shí)機(jī)

? ? shakeAnimation.autoreverses=YES;

? ? shakeAnimation.duration=0.5;


? ? // 創(chuàng)建動(dòng)畫組

? ? CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

? ? animationGroup.animations=@[shakeAnimation];

? ? animationGroup.duration=2.0;

? ? animationGroup.repeatCount=HUGE_VALF;


? ? [self.layer removeAnimationForKey:@"ScaleAnimation"];

? ? [self.layer addAnimation:animationGroup forKey:@"ScaleAnimation"];

}


/// 添加白光閃過動(dòng)畫

- (void) startWhiteLightAnimation{


? ? for(CALayer*layer?in?self.layer.sublayers){

? ? ? ? if([layer.name isEqualToString:@"whiteLightLayer"]){

? ? ? ? ? ? [layer removeFromSuperlayer];

? ? ? ? ? ? break;

? ? ? ? }

? ? }


? ? CALayer*whiteLightLayer = [CALayer layer];

? ? whiteLightLayer.backgroundColor= [[UIColor whiteColor] colorWithAlphaComponent:0.7].CGColor;

? ? whiteLightLayer.frame=CGRectMake(5,0,self.frame.size.width-10,3);

? ? whiteLightLayer.opacity=0.0;

? ? whiteLightLayer.name=@"whiteLightLayer";

? ? [self.layer addSublayer:whiteLightLayer];


? ? // 創(chuàng)建一個(gè) CATransform3D 對(duì)象來設(shè)置傾斜的角度

? ? CATransform3D transform = CATransform3DIdentity;

? ? transform =CATransform3DRotate(transform, -M_PI_4,0.0,0.0,1.0);

? ? whiteLightLayer.transform= transform;


? ? // 改變位置動(dòng)畫

? ? CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

? ? positionAnimation.fromValue= [NSValue valueWithCGPoint:CGPointMake(0,0)];

? ? positionAnimation.toValue= [NSValue valueWithCGPoint:CGPointMake(self.frame.size.width,self.frame.size.height)];

? ? positionAnimation.duration=0.5;

? ? positionAnimation.autoreverses=YES;


? ? // 改變透明度動(dòng)畫

? ? CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];

? ? opacityAnimation.fromValue=@(1.0);

? ? opacityAnimation.toValue=@(0.0);

? ? opacityAnimation.duration=0.5;


? ? CAAnimationGroup *animationGroup = [CAAnimationGroup animation];

? ? animationGroup.animations=@[opacityAnimation, positionAnimation];

? ? animationGroup.duration=1.5;// 持續(xù)時(shí)間

? ? animationGroup.repeatCount=HUGE_VALF;


? ? [whiteLightLayer addAnimation:animationGroup forKey:@"WhiteLightAnimation"];

}

最后編輯于
?著作權(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)畫篇之CoreAnimation動(dòng)畫 9月 22, 2016發(fā)布在Objective-C App如果想被大...
    白水灬煮一切閱讀 2,310評(píng)論 0 0
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,270評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,694評(píng)論 6 30
  • 轉(zhuǎn)自:http://blog.csdn.net/zhibudefeng/article/details/86915...
    咖啡綠茶1991閱讀 751評(píng)論 0 2
  • 1、Core Animation Core Animation,即為核心動(dòng)畫,它是一組非常強(qiáng)大的動(dòng)畫處理API,使...
    Geniusn閱讀 537評(píng)論 0 1

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