(抄)iOS實(shí)現(xiàn)動(dòng)畫的幾種常見方式

http://www.devqinwei.com/2015/10/15/ios實(shí)現(xiàn)動(dòng)畫的幾種常見方式/

1. 使用CALayer的基本關(guān)鍵幀動(dòng)畫CABasicAnimation和CAKeyframeAnimation

特點(diǎn):可做3D動(dòng)畫

詳細(xì)介紹可參看兩個(gè)帖子:

http://blog.csdn.net/iosevanhuang/article/details/14488239

http://blog.csdn.net/wscqqlucy/article/details/8669636

注:( CGAffineTransform 和 CATransform3D 的比較 )

CGAffineTransform is used for 2-D manipulation of NSViews, UIViews, and other 2-D Core Graphics elements.

CATransform3D is a Core Animation structure that can do more complex 3-D manipulations of CALayers.( 搬運(yùn)from stackOverFlow)

如:(CABasicAnimation)

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

animation.fromValue = @(0);

animation.toValue = @(-M_PI);

animation.repeatCount = 0;

animation.duration = 0.4;

[aView.layer addAnimation:animation forKey:@"rotation"];

CATransform3D transform = CATransform3DIdentity;

transform.m34 = 1.0 / 500.0;

aView.layer.transform = transform;

如:(CAKeyframeAnimation)

CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];

keyFrameAnimation.delegate = self;

keyFrameAnimation.duration = 1.0f;

keyFrameAnimation.beginTime = CACurrentMediaTime() + 1.0f;

NSValue *initialBounds = [NSValue valueWithCGRect: self.navigationController.view.layer.mask.bounds];

NSValue *secondBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, self.navigationController.view.layer.mask.bounds.size.width - 40.0f , self.navigationController.view.layer.mask.bounds.size.height - 40.0f)];

NSValue *finalBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, 2000.0f, 2000.0f)];

keyFrameAnimation.values = @[initialBounds,secondBounds,finalBounds];

keyFrameAnimation.keyTimes = @[@0, @0.5, @1];

keyFrameAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] ];

keyFrameAnimation.removedOnCompletion = NO;

keyFrameAnimation.fillMode = kCAFillModeForwards;

[self.navigationController.view.layer.mask addAnimation:keyFrameAnimation forKey:@"maskAnimation"];

2. 使用CALayer的事務(wù)類CATransaction來(lái)執(zhí)行

CATransaction動(dòng)畫包含顯示動(dòng)畫和隱式動(dòng)畫兩種。(顯示能控制動(dòng)畫中更多方面)

隱式動(dòng)畫:

//設(shè)置變化動(dòng)畫過(guò)程是否顯示,默認(rèn)為YES不顯示

[CATransaction setDisableActions:NO];

//設(shè)置圓角

self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;

//設(shè)置透明度

self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;

顯示動(dòng)畫:

//修改執(zhí)行時(shí)間

[CATransaction begin];

//顯式事務(wù)默認(rèn)開啟動(dòng)畫效果,kCFBooleanTrue關(guān)閉

[CATransaction setValue:(id)kCFBooleanFalse

forKey:kCATransactionDisableActions];

//動(dòng)畫執(zhí)行時(shí)間

[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];

//[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]];

self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;

self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;

[CATransaction commit];

另外,CATransaction來(lái)可以用來(lái)設(shè)置嵌套動(dòng)畫,即先執(zhí)行A動(dòng)畫,再執(zhí)行B動(dòng)畫,再執(zhí)行C動(dòng)畫…(這里不做舉例)

(例子來(lái)源于:http://www.cnblogs.com/bandy/archive/2012/03/26/2418165.html)

3. 使用UIView關(guān)鍵幀動(dòng)畫animateKeyframesWithDuration

如:

[UIView animateKeyframesWithDuration:durationTime delay:0 options:0 animations:^{

[UIView addKeyframeWithRelativeStartTime:0*frameDuration relativeDuration:1*frameDuration animations:^{

self.pickGradeView.transform = CGAffineTransformMakeScale(0.01, 0.01);

}];

[UIView addKeyframeWithRelativeStartTime:1*frameDuration relativeDuration:1*frameDuration animations:^{

self.pickGradeView.alpha = 1.0;

}];} completion:^(BOOL finished) {

}];

4.使用UIView animateWithDuration

如:

[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.5 options:0 animations:^{

self.shortNameLabel.transform = CGAffineTransformMakeScale(0.8, 0.8);

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{

self.shortNameLabel.transform = CGAffineTransformMakeScale(1.0, 1.0);

} completion:nil];

}];

最后編輯于
?著作權(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)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,694評(píng)論 6 30
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過(guò)程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,270評(píng)論 5 13
  • 在iOS實(shí)際開發(fā)中常用的動(dòng)畫無(wú)非是以下四種:UIView動(dòng)畫,核心動(dòng)畫,幀動(dòng)畫,自定義轉(zhuǎn)場(chǎng)動(dòng)畫。 1.UIView...
    請(qǐng)叫我周小帥閱讀 3,326評(píng)論 1 23
  • - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *...
    七里田間的守望者閱讀 1,529評(píng)論 0 4
  • 前言 本文只要描述了iOS中的Core Animation(核心動(dòng)畫:隱式動(dòng)畫、顯示動(dòng)畫)、貝塞爾曲線、UIVie...
    GitHubPorter閱讀 3,742評(píng)論 7 11

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