UIView 中动画相关属性及方法
___________________________________________________________________________________
@interface UIView(UIViewAnimation)
//开始动画
+ (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested
//提交动画
+ (void)commitAnimations; // starts up any animations when the top level animation is commited
// no getters. if called outside animation block, these setters have no effect.
//设置动画的代理
+ (void)setAnimationDelegate:(nullable id)delegate; // default = nil
//设置动画将要开始使用的时候调用的方法
+ (void)setAnimationWillStartSelector:(nullable SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
// 设置动画已经结束的时候调用的方法
+ (void)setAnimationDidStopSelector:(nullable SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
//设置动画持续时间
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2
//设置动画的延迟时间(即等待多久后执行动画)
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0
//设置动画的开始时间
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])
//设置动画加速和减速的
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut
//设置动画重复次数
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional
//YES:逆向(相反)动画效果,结束后返回动画逆向前的状态, 默认 NO
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero
//设置动画是否从当前状态开始
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).
//设置动画过度效果,transition动画的过度效果,view过度效果作用的视图,cache(YES:开始和结束视图分别渲染一次并在动画中创建帧,NO:视图将会渲染每一帧)
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // current limitation - only one per begin/commit block
//设置是否可以进行动画
+ (void)setAnimationsEnabled:(BOOL)enabled; // ignore any attribute changes while set.
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) BOOL areAnimationsEnabled;
#else
+ (BOOL)areAnimationsEnabled;
#endif
//动画是否已结束
+ (void)performWithoutAnimation:(void (NS_NOESCAPE ^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) NSTimeInterval inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);
#else
+ (NSTimeInterval)inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);
#endif
@end
___________________________________________________________________________________
@interface UIView(UIViewAnimationWithBlocks)
/**
* 持续动画
*
* @param duration 动画持续时间
* @param delay 动画延迟开始时间
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param animations 动画开始Block
* @param completion 动画结束回调
*/
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
/**
* 持续动画
*
* @param duration 动画持续时间
* @param animations 动画开始Block
* @param completion 动画结束回调
*/
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
/**
* 持续动画
*
* @param duration 动画持续时间
* @param animations 动画开始Block
*/
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
/**
* 弹簧动画
*
* @param duration 动画持续时间
* @param delay 动画延迟时间
* @param dampingRatio 阻尼值(0.0~1.0),阻尼值越小,弹动的幅度越大,如果大道一定程度,会出现弹不动的情况。
* @param velocity 速率,值越小,弹簧拉伸的幅度越小。这里需要注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果。
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param completion 动画结束回调
*/
+ (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);
/**
* 转场动画
*
* @param view 动画操作对象
* @param duration 动画持续时间
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param animations 动画开始Block
* @param completion 动画结束回调
*/
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
/**
* 转场动画
*
* @param fromView 当前视图
* @param toView 转场后视图
* @param duration 动画持续时间
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param completion 动画结束回调
*/
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview
/**
* 系统动画
*
* @param duration 动画持续时间
* @param delay 动画延迟时间
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param animations 动画开始Block
* @param completion 动画结束回调
*/
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindof UIView *> *)views options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
@end
___________________________________________________________________________________
@interface UIView (UIViewKeyframeAnimations)
/**
* 关键帧动画
*
* @param duration 动画持续时间
* @param delay 动画延迟时间
* @param options 动画属性 /时间函数曲线相关 /转场动画相关
* @param animations 动画开始Block
* @param completion 动画结束回调
*/
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
/**
* 关键帧动画
*
* @param frameStartTime 相对开始时间
* @param frameDuration 相对持续时间
* @param animations 动画开始Block
*/
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0); // start time and duration are values between 0.0 and 1.0 specifying time and duration relative to the overall time of the keyframe animation
@end
————————
关键帧动画实例
- (void)runAnimateKeyframes:(UIView *)view {
/**
* relativeDuration 动画在什么时候开始
* relativeStartTime 动画所持续的时间
*/
[UIView animateKeyframesWithDuration:8.f
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeLinear
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 // 相对于8秒所开始的时间(第0秒开始动画)
relativeDuration:1/4.0 // 相对于8秒动画的持续时间(动画持续2秒)
animations:^{
view.backgroundColor = [UIColor purpleColor];
view.size = CGSizeMake(40, 40);
}];
[UIView addKeyframeWithRelativeStartTime:1/4.0 // 相对于8秒所开始的时间(第2秒开始动画)
relativeDuration:1/4.0 // 相对于8秒动画的持续时间(动画持续2秒)
animations:^{
view.backgroundColor = [UIColor yellowColor];
view.size = CGSizeMake(20, 20);
}];
[UIView addKeyframeWithRelativeStartTime:2/4.0 // 相对于8秒所开始的时间(第4秒开始动画)
relativeDuration:1/4.0 // 相对于8秒动画的持续时间(动画持续2秒)
animations:^{
view.backgroundColor = [UIColor greenColor];
view.size = CGSizeMake(40, 40);
}];
[UIView addKeyframeWithRelativeStartTime:3/4.0 // 相对于8秒所开始的时间(第4秒开始动画)
relativeDuration:1/4.0 // 相对于8秒动画的持续时间(动画持续2秒)
animations:^{
view.backgroundColor = [UIColor redColor];
view.size = CGSizeMake(20, 20);
}];
}
completion:^(BOOL finished) {
[self runAnimateKeyframes:view];
}];
}