使用CoreAnimation 實現相機拍攝照片之後動畫效果,

來源:互聯網
上載者:User

使用CoreAnimation 實現相機拍攝照片之後動畫效果,

廢話不多說,先看上效果,由於動畫錄製的時候幀率限制,只能將動畫放慢了進行錄製,更容易看到效果

這是點擊開始之後代碼

-(IBAction)btnStartClick:(id)sender{      CABasicAnimation *baseanimation1=[CABasicAnimation animationWithKeyPath:@"transform.scale.x"];    baseanimation1.fromValue=@(1.0f);    baseanimation1.toValue=@(20.0f/myview.frame.size.width);        CABasicAnimation *baseanimation2=[CABasicAnimation animationWithKeyPath:@"transform.scale.y"];    baseanimation2.fromValue=@(1.0f);    baseanimation2.toValue=@(20.0f/myview.frame.size.height);        CAAnimationGroup *cg=[CAAnimationGroup animation];    cg.duration=0.2;    cg.animations=@[baseanimation1,baseanimation2];    cg.repeatCount=0;    cg.delegate=self;    cg.removedOnCompletion=NO;    [myview.layer addAnimation:cg forKey:@"myviewscale"];}

 由於動畫使由多個動畫組成,所以第一個動畫完畢之後自動再次開始一個動畫

-(void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    if(anim==[myview.layer animationForKey:@"myviewscale"])    {        myview.frame=CGRectMake((self.view.frame.size.width-20)/2,(self.view.frame.size.height-20)/2, 20, 20);        [myview.layer removeAnimationForKey:@"myviewscale"];        UIBezierPath *path=[UIBezierPath bezierPath];        [path moveToPoint:self.view.center];        [path addQuadCurveToPoint:CGPointMake(20, self.view.frame.size.height-20) controlPoint:CGPointMake(35, 50)];                CAKeyframeAnimation *keyframeanimation1=[CAKeyframeAnimation animationWithKeyPath:@"position"];        keyframeanimation1.path=path.CGPath;        keyframeanimation1.duration=1;        //keyframeanimation1.rotationMode = kCAAnimationRotateAuto;        keyframeanimation1.rotationMode = nil;        keyframeanimation1.delegate=self;        keyframeanimation1.removedOnCompletion=NO;        keyframeanimation1.fillMode = kCAFillModeForwards;        keyframeanimation1.timingFunction=[CAMediaTimingFunction functionWithControlPoints:0.3 :0.7 :0.7 :0.3];        [myview.layer addAnimation:keyframeanimation1 forKey:@"myviewposition"];    }    else    {        myview.frame=CGRectMake(10,self.view.frame.size.height-20, 20, 20);        [myview.layer removeAnimationForKey:@"myviewposition"];    }}

 

先解釋一下動畫執行過程

第一步是通過CABasicAnimation 對照片進行縮放

第二步是通過CAKeyframeAnimation 對照片進行位移並最終產生拋物線的投擲效果

 

拋物線軌跡的實現

要想讓動畫有一個拋物線的軌跡就需要一個二次方貝茲曲線的軌跡進行類比,二次方貝茲曲線如下,這裡引用了一個網路的動畫圖片。

 

二次方貝茲曲線中 p0 為起始點、p1 為控制點、 p2為結束點

 

UIBezierPath 的執行個體方法 addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;要求輸入一個結束點(p2) 和 控制點(p1),然後這個函數將為我們自動構建一個二次方貝茲曲線。

 

拋物線速度控制

拋物線速度前半段由快變慢,後半段由慢變快是由CAKeyframeAnimation 的 timingFunction 進行控制的,timingFunction是CAMediaTimingFunction類型的。CAMediaTimingFunction可以通過functionWithName:(NSString *)name 進行初始化,輸入值 name的取值範圍包括 linear(數值平均增長)、easeIn(先快後慢)、easeOut(先慢後快)、easeInEaseOut(先快後慢先慢後快)。也可以通過 functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y 進行初始化,輸入值為三次貝茲路徑的兩個控制點座標,控制點座標取值範圍都是在[0,1]之間。因此通過functionWithName函數是無法滿足我們的需求的,那麼只能使用函數functionWithControlPoints了。

 

functionWithControlPoints所使用的三次貝茲路徑大概可以使用進行表示(圖片來源於網路,請忽略圖片上面的橫縱座標文字):

 

 

橫向座標定義為需要變化的數值 ,縱座標定義為時間 那麼 p1  和 p2 就是這個函數的兩個輸入的控制點參數

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.