UIView视图的动画功能,可以使在更新或切换视图时有放缓节奏、产生流畅的动画效果,从而达到改善用户的体验。
UIView 可以产生动画效果的变化包括:
位置变化:在屏幕上移动视图。
大小变化:改变视图框架(frame)和边界。
拉伸变化:改变视图内容的延展区域。
改变透明度:改变视图的alpha值。
改变状态:隐藏或显示状态。
改变视图层次顺序:视图哪个前哪个后。
旋转:即任何应用到视图上的仿射变换(transform)。
一、必须熟悉的枚举变量
// UIViewAnimationCurve 是用来定义动画加速或减速方式(学术点:设置动画曲线)
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // slow at beginning and end(先慢后快再慢)
UIViewAnimationCurveEaseIn, // slow at beginning(先慢后快)
UIViewAnimationCurveEaseOut, // slow at end(先快后慢)
UIViewAnimationCurveLinear // 做线性的变化(有动画效果, 但是动画的执行的速度是均匀的)
};
// UIViewAnimationTransition 用来设置动画的过渡效果
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone, // 这个是正常的(也就是没有过渡效果)
UIViewAnimationTransitionFlipFromLeft, // 从左向右翻转
UIViewAnimationTransitionFlipFromRight, // 从右向左翻转
UIViewAnimationTransitionCurlUp, // 从下向上卷(效果类似正着翻书)
UIViewAnimationTransitionCurlDown, // 从上向下卷(效果类似于反着翻书)
};
// 动画选项枚举 UIViewAnimationOptions
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // do not inherit any options or animation type
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
} NS_ENUM_AVAILABLE_IOS(4_0);
// 这个是用来设置关键帧动画
typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) {
UIViewKeyframeAnimationOptionLayoutSubviews = UIViewAnimationOptionLayoutSubviews,
UIViewKeyframeAnimationOptionAllowUserInteraction = UIViewAnimationOptionAllowUserInteraction, // turn on user interaction while animating
UIViewKeyframeAnimationOptionBeginFromCurrentState = UIViewAnimationOptionBeginFromCurrentState, // start all views from current value, not initial value
UIViewKeyframeAnimationOptionRepeat = UIViewAnimationOptionRepeat, // repeat animation indefinitely
UIViewKeyframeAnimationOptionAutoreverse = UIViewAnimationOptionAutoreverse, // if repeat, run animation back and forth
UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, // ignore nested duration
UIViewKeyframeAnimationOptionOverrideInheritedOptions = UIViewAnimationOptionOverrideInheritedOptions, // do not inherit any options or animation type
UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, // default
UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10,
UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10
} NS_ENUM_AVAILABLE_IOS(7_0);
二、UIView 的动画接口
1、最基本的接口 ( 可以不看 )
@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).
/**
* 设置动画过渡效果
*
* @param transition 动画的过渡效果
* @param view 过渡效果作用视图
* @param cache 如果为YES,开始和结束视图分别渲染一次并在动画中创建帧;否则,视图将会渲染每一帧。例如,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。
*/
+ (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.
+ (BOOL)areAnimationsEnabled;
// 动画是否已经结束
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);
+ (NSTimeInterval)inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);
@end
1.1动画的使用方式是首尾式的
[UIView beginAnimations:nil context:nil]; // 开始动画
// Code...(重要填写需要执行的动画的代码)
[UIView commitAnimations]; // 提交动画
[UIView beginAnimations:nil context:nil]; // 开始动画
[UIView setAnimationDuration:0.5]; // 动画时长
/** 图像向右下移动 */
CGPoint point = self.myView.center;
point.y += 50;
point.x += 50;
[self.myView setCenter:point];
[UIView commitAnimations]; // 提交动画
2、block接口
@interface UIView(UIViewAnimationWithBlocks)
+ (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);
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
+ (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);
+ (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);
+ (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
+ (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
####3、关键帧动画接口
@interface UIView (UIViewKeyframeAnimations)
+ (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);
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0);
@end
网友评论