AXAnimationChain是一個鏈式動畫庫
,能夠用來輕鬆的建立基於CAAnimation
的鏈式動畫。鏈的組合方式有兩種,一種是組合,另外一種則是連接,經過以上兩種方式建立的動畫,既能夠同時進行,也能夠按時間前後進行,可使用較少的代碼建立出豐富複雜的動畫效果:git
簡單使用:github
_transitionView.spring.centerBy(CGPointMake(0, 100)).easeOut.spring.sizeBy(CGSizeMake(100, 100)).spring.cornerRadiusBy(4).animate();
高級使用:objective-c
_transitionView.chainAnimator.basic.target(self).complete(@selector(complete:)).property(@"position").toValue([NSValue valueWithCGPoint:CGPointMake(100, self.view.center.y)]).easeInBack.duration(0.5).combineSpring.target(self).complete(@selector(complete:)).property(@"bounds").toValue([NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)]).duration(0.5).repeatCount(5).autoreverses.combineSpring.target(self).complete(@selector(complete:)).property(@"transform.rotation").toValue(@(M_PI_4)).duration(0.5).repeatCount(3).beginTime(1.0).autoreverses.nextToBasic.property(@"position").toValue([NSValue valueWithCGPoint:self.view.center]).duration(0.5).combineSpring.property(@"bounds").toValue([NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)]).duration(0.8).nextToBasic.property(@"transform.rotation").toValue(@(M_PI_4)).duration(1.0).completeWithBlock(nil).animate();
看起來比較冗餘,可是細讀會發現,其實就只有一行代碼.spring
連接和組合在協議AXAnimatorChainDelegate
中進行定義,分別是:nextTo:
和combineWith:
,在使用的過程當中應當予以區分. 安全
AXAnimationChain
基於CoreAnimation
定義了幾種Animator
,AXChainAnimator
是基類,預約義了一系列Animate
操做,能夠連接、組合而且控制動畫完成的回調:併發
AXChainAnimator --AXBasicChainAnimator ==CABasicAnimation --AXSpringChainAnimator ==CASpringAnimation --AXKeyframeChainAnimator ==CAKeyframeAnimation --AXTransitionChainAnimator==CATransitionAnimation
經過連接的方式處理兩個animator
,被連接的animator
將會在前者動畫(包含組合
的動畫)完成以後進行動畫, 大概的示例以下:框架
[former nextTo:nexter];
Next-To
方法的原型以下:ide
- (instancetype)nextTo:(id<AXAnimatorChainDelegate>)animator;
當向former aniamtor
發送nextTo:
消息以後,返回的是nexter animator
做爲下次連接或者組合操做的對象,所以AXAnimationChain
定義了幾種經常使用的操做:動畫
/// 連接到Basic動畫而且返回連接的Basic動畫. - (AXBasicChainAnimator *)nextToBasic; /// 連接到Spring動畫而且放回連接的Spring動畫. - (AXSpringChainAnimator *)nextToSpring; /// 連接到Keyframe動畫而且放回連接的Keyframe動畫. - (AXKeyframeChainAnimator *)nextToKeyframe; /// 連接到Transition動畫而且返回連接的Transition動畫. - (AXTransitionChainAnimator *)nextToTransition;
在發送消息以後分別返回對應類型的可操做對象.ui
經過組合的方式處理兩個animator
,被組合的animator
將會與前者動畫同時進行,完成的時間以時間最長的爲準, 示例以下:
[former combineWith:combiner];
Combine-With
方法原型以下:
- (instancetype)combineWith:(nonnull AXChainAnimator *)animator;
當向former animator
發送combineWith:
消息以後,返回的是combiner animator
做爲下次連接或者組合操做的對象,在AXAnimationChain
中,默認一下幾種組合方式:
/// 組合到Basic動畫而且返回組合的Basic動畫. - (AXBasicChainAnimator *)combineBasic; /// 組合到Spring動畫而且放回組合的Spring動畫. - (AXSpringChainAnimator *)combineSpring; /// 組合到Keyframe動畫而且放回組合的Keyframe動畫. - (AXKeyframeChainAnimator *)combineKeyframe; /// 組合到Transition動畫而且返回組合的Transition動畫. - (AXTransitionChainAnimator *)combineTransition;
一樣的,在向某一操做對象animator
發送以上消息以後,將會分別返回對應類型的可操做對象.
在AXAnimationChain
中,關係的管理採用的是二叉樹的理論. 某一個animator
對應的類結構中,包含了指向父節點的superAnimator
用於表示父animator
, 表示此animator
爲superAnimator
所連接的animator
, 此時,superAnimator
的childAnimator
即指向此animator
做爲一個閉環鏈將二者的關係鎖定起來; 一樣的,某一個animator
還擁有一個指向兄弟節點的NSArray<AXChainAnimator *>
結構:combinedAnimators
用於管理所組合的animators
,而且,被組合的animator
的父節點superAnimator
則指向當前animator
.
- (void)start { NSAssert(_animatedView, @"Animation chain cannot be created because animated view is null."); AXChainAnimator *superAnimator = _superAnimator; AXChainAnimator *superSuperAnimator = _superAnimator; while (superAnimator) { superAnimator = superAnimator.superAnimator; if (superAnimator) { superSuperAnimator = superAnimator; } } if (superSuperAnimator) { [superSuperAnimator start]; } else { [self _beginAnimating]; if (!_childAnimator) [self _clear]; } }
AXAnimatioChain
就是經過這樣的關係把全部連接和組合的animator
管理起來的,在完成關係的連接或組合以後,須要向最後一個animator
發送-start
消息動畫才能正常進行. animator
在接收到-start
消息以後,會逐級遍歷superAnimator
直至superAnimator.superAnimator==nil
, 此時獲取到superSuperAnimator
, 從superSuperAnimator
自祖先往下逐級進行動畫,組合的動畫會同時進行,連接的動畫則按順序進行.
輕量級解決方案
基於CoreAnimation的封裝,安全、高效!
一行代碼搞定複雜的動畫管理,提升代碼維護效
時間曲線,時間曲線用於描述動畫隨時間進行的速度,AXAnimationChain
除了包含系統默認的時間曲線以外,還提供了以下的曲線以呈現更漂亮的動畫:
CoreAnimation
自iOS2.0
就爲iOS平臺提供了核心動畫的支持,可是在iOS9.0以前,一直沒有Spring
動畫,要使用Spring
動畫要麼使用第三方動畫庫,要麼使用系統提供的方法:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
可是系統提供的這個方法也是iOS7.0
之後才能使用了,而且在控制上並不是那麼容易.
AXSpringAnimation
是基於阻尼震動運動模型的Spring
動畫類,可以完美與CASpringAnimation
相通用:
動畫中,左邊正方形使用的是CASpringAnimation
類,右邊的則使用的是AXSpringAnimation
,二者的動畫曲線是一致的.
一樣地,AXSpringAnimation
的API和CASpringAnimation
也是一致的:
@interface AXSpringAnimation : CAKeyframeAnimation /* The mass of the object attached to the end of the spring. Must be greater than 0. Defaults to one. */ @property(assign, nonatomic) CGFloat mass; /* The spring stiffness coefficient. Must be greater than 0. * Defaults to 100. */ @property(assign, nonatomic) CGFloat stiffness; /* The damping coefficient. Must be greater than or equal to 0. * Defaults to 10. */ @property(assign, nonatomic) CGFloat damping; /* The initial velocity of the object attached to the spring. Defaults * to zero, which represents an unmoving object. Negative values * represent the object moving away from the spring attachment point, * positive values represent the object moving towards the spring * attachment point. */ @property(assign, nonatomic) CGFloat initialVelocity; /* Returns the estimated duration required for the spring system to be * considered at rest. The duration is evaluated for the current animation * parameters. */ @property(readonly, nonatomic) CFTimeInterval settlingDuration; /* The objects defining the property values being interpolated between. * All are optional, and no more than two should be non-nil. The object * type should match the type of the property being animated (using the * standard rules described in CALayer.h). The supported modes of * animation are: * * - both `fromValue' and `toValue' non-nil. Interpolates between * `fromValue' and `toValue'. * * - `fromValue' and `byValue' non-nil. Interpolates between * `fromValue' and `fromValue' plus `byValue'. * * - `byValue' and `toValue' non-nil. Interpolates between `toValue' * minus `byValue' and `toValue'. */ @property(nullable, strong, nonatomic) id fromValue; @property(nullable, strong, nonatomic) id toValue; @property(nullable, strong, nonatomic) id byValue; @end
AXAnimationChain
框架還提供了將CABasicAnimation
無縫轉換爲CAKeyframeAnimation
的功能:
動畫中,左邊是CABasicAnimation
,右邊是CAKeyframeAnimation
,二者對應的動畫曲線是一致的.
要使用動畫轉換,請參考:
#import <QuartzCore/QuartzCore.h> #import <UIKit/UIKit.h> #import "CAMediaTimingFunction+Extends.h" @interface CAAnimation (Convertable) @end @interface CAKeyframeAnimation (Convertable) + (instancetype)animationWithBasic:(CABasicAnimation *)basicAnimation; + (instancetype)animationWithBasic:(CABasicAnimation *)basicAnimation usingValuesFunction:(double (^)(double t, double b, double c, double d))valuesFunction; @end
AXAnimationChain
對系統版本支持到iOS8.0
,須要使用到的框架:
Foundation.framework
UIKit.framework
QuartzCore.framework
使用的時候最好使用最新版Xcode.
AXAimationChain
To Your ProjectCocoaPods) is the recommended way to add AXAimationChain to your project.
Add a pod entry for AXAimationChain to your Podfile pod 'AXAimationChain', '~> 0.1.0'
Install the pod(s) by running pod install
.
Include AXAimationChain wherever you need it with #import "AXAimationChain.h"
.
若須要單獨使用AXSpringAnimation
或者Convertable
以及TimingControl
等特性的話,只須要將podfile裏邊AXAnimationChain
替換爲AXAnimationChain/CoreAnimation
便可,即:pod 'AXAimationChain/CoreAnimation', '~> 0.1.0'
.
Alternatively you can directly add all the source files to your project.
Download the latest code version) or add the repository as a git submodule to your git-tracked project.
Open your project in Xcode, then drag and drop the source group onto your project (use the "Product Navigator view"). Make sure to select Copy items when asked if you extracted the code archive outside of your project.
Include AXAnimationChain wherever you need it with #import "AXAimationChain.h"
.
如單獨使用AXSpringAnimation
或者Convertable
以及TimingControl
等特性,只須要導入#import "AXCoreAnimation.h"
便可.
This code is distributed under the terms and conditions of the MIT license.
請參考示例工程代碼以及API.
此項目在開展的時候比較龐大,基礎的核心類已經構建好了,基本目標已經達成,可是還有不少須要完善的地方,後邊會逐步完善併發布Release版本.
轉載需註明出處:http://devedbox.com/AXAnimationChain/