Summary
AXAnimationChain is a chain animation library that can be used to easily create CAAnimation based chain animations. There are two ways to combine chains, one is to combine them, the other is to link them. Animations created by the above two ways can be done simultaneously or in chronological order, which can create rich and complex animation effects with less code:
Simple use:
_transitionView.spring.centerBy(CGPointMake(0, 100)).easeOut.spring.sizeBy(CGSizeMake(100, 100)).spring.cornerRadiusBy(4).animate();Copy the code
Advanced use:
_transitionView.chainAnimator.basic.target(self).complete(@selector(complete:)).property(@"position").toValue([NSValue valueWithCGPoint:CGPointMake(100, Self. View. Center. Y)]). EaseInBack. Duration (0.5). The combineSpring. The target (self) complete (@ the selector (complete:)). The property (@ "boun ds").toValue([NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)]). Duration (0.5). The repeatCount (5). Autoreverses.com bineSpring. The target (self) complete (@ the selector (complete:)). The property (@ "tr Ansform. Rotation "). ToValue (@ (M_PI_4)). The duration (0.5). The repeatCount (3) beginTime (1.0). The autoreverses. NextToBasic. Property (@ "Po sition").toValue([NSValue ValueWithCGPoint: self view. The center]). Duration (0.5). The combineSpring. Property (@ "bounds"). ToValue ([NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)]). Duration (0.8). The nextToBasic. Property (@ "transform. Rotation".) toValue (@ (M_PI_4)). Duration (1.0). The completeWithBlock (nil) .animate();Copy the code
It looks redundant, but if you look closely, it’s really just one line of code.
Links and combinations are defined in the AXAnimatorChainDelegate protocol: nextTo: and combineWith:, respectively. They should be distinguished during use.
AXChainAnimator is a base class that defines a series of Animate actions that link, combine, and control the completion of an animation:
AXChainAnimator
--AXBasicChainAnimator ==CABasicAnimation
--AXSpringChainAnimator ==CASpringAnimation
--AXKeyframeChainAnimator ==CAKeyframeAnimation
--AXTransitionChainAnimator==CATransitionAnimationCopy the code
Next-To
Handle two animators by linking them. The linked animator will animate after the former animation (including the combined animation) is complete, as shown in the following example:
[former nextTo:nexter];Copy the code
The prototype of the Next-to method is as follows:
- (instancetype)nextTo:(id<AXAnimatorChainDelegate>)animator;Copy the code
When a nextTo: message is sent to former Aniamtor, the Nexter Animator is returned as the object for the next link or combination operation, so AXAnimationChain defines several common operations:
/// link to Basic animation and return linked Basic animation. - (AXBasicChainAnimator *)nextToBasic; - (AXSpringChainAnimator *)nextToSpring; - (AXKeyframeChainAnimator *)nextToKeyframe; - (AXKeyframeChainAnimator *)nextToKeyframe; / / / and return link links to the Transition animation of the Transition animation. - (AXTransitionChainAnimator *) nextToTransition;Copy the code
After the message is sent, the operable object of the corresponding type is returned.
Combine-With
Process two animators by combining them. The combined animator will run concurrently with the first animator, taking the longest time to complete, as shown in the following example:
[former combineWith:combiner];Copy the code
The prototype of the Combining-with method is as follows:
- (instancetype)combineWith:(nonnull AXChainAnimator *)animator;Copy the code
When sending the combineWith: message to the former Animator, the combiner Animator is returned as the object of the next link or combination operation. In the AXAnimationChain, the default combinations are as follows:
/// combine to Basic animation and return the combined Basic animation. - (AXBasicChainAnimator *)combineBasic; - (AXSpringChainAnimator *)combineSpring; - (AXKeyframeChainAnimator *)combineKeyframe; / / / and returns to the Transition animation combination of the Transition animation. - (AXTransitionChainAnimator *) combineTransition;Copy the code
Similarly, after sending the above message to an animator, operable objects of the corresponding type will be returned.
Relationship
In AXAnimationChain, relations are managed by binary tree theory. The class structure of an animator contains the superAnimator pointing to the parent node. The superAnimator is the parent animator, indicating that the animator is the animator linked by the superAnimator. SuperAnimator’s childAnimator points to the animator as a closed loop that locks the relationship between the two. Similarly, an animator also has an NSArray
structure that points to sibling nodes :combinedAnimators for managing the combinedAnimators, and, The parent of the combined animator, the superAnimator, points to the current 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];
}
}Copy the code
The AXAnimatioChain manages all linked and combined animators using these relationships. After the link or combination is complete, the animator needs to send a -start message to the last animator. Will traverse superAnimator step by step until superAnimator. SuperAnimator = = nil, now get superSuperAnimator, step by step down from a superSuperAnimator from ancestors for animation, Combined animations are performed simultaneously, and linked animations are performed sequentially.
Features
- Lightweight solution
- CoreAnimation based packaging, safe and efficient!
- One line of code to solve the complex animation management, improve code maintenance efficiency
TimingControl
Time curves are used to describe the speed of animation over time. AXAnimationChain includes the system’s default time curves as well as the following curves to render more beautiful animations:
AXSpringAnimation
CoreAnimation has provided CoreAnimation support for iOS platforms since iOS2.0, but before iOS9.0, there was no Spring animation. To use Spring animation, either use a third-party animation library or use the method provided by the system:
+ (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);Copy the code
However, this method is not available until after iOS7.0, and it is not easy to control.
AXSpringAnimation is a Spring animation class based on the damped vibration motion model, which can be perfectly interlinked with CASpringAnimation:
In the animation, the left square uses CASpringAnimation, and the right square uses AXSpringAnimation. The animation curves of the two are consistent.
Likewise, AXSpringAnimation’s API is the same as CASpringAnimation’s:
@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;
@endCopy the code
Convertable
The AXAnimationChain framework also provides the ability to seamlessly convert a CABasicAnimation to a CAKeyframeAnimation:
In the animation, the left side is CABasicAnimation and the right side is CAKeyframeAnimation. Their corresponding animation curves are consistent.
To use animated transformations, see:
#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;
@endCopy the code
Requirements
AXAnimationChain supports OS version to iOS8.0, using the following framework:
-
Foundation.framework
-
UIKit.framework
- QuartzCore.framework
It is best to use the latest version of Xcode.
Adding AXAimationChain
To Your Project
CocoaPods
CocoaPods is the recommended way to add AXAimationChain to your project.
- Add a pod entry for AXAimationChain to your Podfile
Pod 'AXAimationChain', '~ > 0.1.0 from'
- Install the pod(s) by running
pod install
. - Include AXAimationChain wherever you need it with
#import "AXAimationChain.h"
. - Use alone if necessary
AXSpringAnimation
orConvertable
As well asTimingControl
And so on, just put it in your podfileAXAnimationChain
Replace withAXAnimationChain/CoreAnimation
Can, namely:Pod 'AXAimationChain/CoreAnimation', '~ > 0.1.0 from'
.
Source files
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"
. - If used alone
AXSpringAnimation
orConvertable
As well asTimingControl
And other features, only need to import#import "AXCoreAnimation.h"
Can.
License
This code is distributed under the terms and conditions of the MIT license.
use
Refer to the sample project code and apis.
insufficient
This project was relatively large when it was carried out. The basic core classes have been built and the basic objectives have been achieved, but there are still many areas to be improved, which will be gradually improved and released later.
The statement
Reprint must indicate the source: http://devedbox.com/AXAnimationChain/