iOS开发高级进阶(8-10)-动画

(还是没搞懂。。。继续看参考中)
交互设计的一部分:布局、变换

UIKit

UIView提供的动画支持

  • (UIViewAnimation):depricated(过时)

  • UIView(UIViewAnimationWithBlocks)
    //修改animations参数,可以实现动画变换
    + (void)animateWithDuration:(NSTimeInterval)duration
    delay:(NSTimeInterval)delay
    options:(UIViewAnimationOptions)options
    animations:(void (^)(void))animations
    completion:(void (^ __nullable)(BOOL finished))completion;

    + (void)animateWithDuration:(NSTimeInterval)duration 
                             animations:(void (^)(void))animations
                             completion:(void (^ __nullable)(BOOL finished))completion
    
    + (void)animateWithDuration:(NSTimeInterval)duration 
                            animations:(void (^)(void))animations 
    
    //Spring Animation, dampingRatio阻尼,velocity初速度
    + (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 ;
    
    + (void)transitionWithView:(UIView *)view 
                            duration:(NSTimeInterval)duration 
                            options:(UIViewAnimationOptions)options 
                            animations:(void (^ __nullable)(void))animations 
                            completion:(void (^ __nullable)(BOOL finished))completion;
    
    + (void)transitionFromView:(UIView *)fromView 
                         toView:(UIView *)toView 
                         duration:(NSTimeInterval)duration 
                         options:(UIViewAnimationOptions)options 
                         completion:(void (^ __nullable)(BOOL finished))completion;
    
    + (void)performSystemAnimation:(UISystemAnimation)animation 
                    onViews:(NSArray<__kindof UIView *> *)views 
                    options:(UIViewAnimationOptions)options 
                    animations:(void (^ __nullable)(void))parallelAnimations 
                    completion:(void (^ __nullable)(BOOL finished))completion;
    

UIView可动画的属性

  • frame
  • bounds
  • center
  • transform
  • alpha
  • backgroundColor
  • contentStretch

动画效果一览

  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,
} ;

UIView的Keyframe动画(可控制中间状态)

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations; // 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

Spring Animation

参考:https://www.renfei.org/blog/ios-8-spring-animation.html

Autolayout环境下的动画

  1. 通过修改constraint实现
    2. 生效[self.view setNeedsUpdateConstraints];
  2. 在更改动画块
    [self.view layoutIfNeeded];

View Transition用动画切换新界面

transitionWithView(子View)

[UIView transitionWithView:(nonnull UIView *)
                       duration:(NSTimeInterval)
                       options:(UIViewAnimationOptions)
                       animations:^(void)animations
                       completion:^(BOOL finished)completion];

//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,
} ;
//animations:block

视图替换(场景变换较大)

[UIView transitionFromView:(nonnull UIView *)
                     toView:(nonnull UIView *)
                     duration:(NSTimeInterval) 
                     options:(UIViewAnimationOptions) 
                     completion:^(BOOL finished)completion];

Core Animation(操纵Layer形成动画效果)

CATransaction

  • 对Layer属性的修改都在CATransaction
  • 修改和生效的是异步的


    过程.png
进一步学习:
  • CATransaction +begin
    
  •  config current transation
    
  • CATransaction +commit
    

Implicit Animation(隐式动画)

  • Core Animation默认是开着的
  • 修改layer的可动画属性时会自动触发动画

Layer可动画的属性

iOS开发高级进阶(8-10)-动画_第1张图片
Layer可动画的属性.png

Todolist:一个一个试试

隐式动画的查找

???什么用?

CAAction

作用:禁止隐式动画(让-actionForKey找不到CAAction)
实现:

//1.  return nil in delegate(CAAction) -actionForLayer:forKey; 
-(id)actionForLayer:(CALayer *)layer forKey:(NSString *)event{
    return  nil;
}
//2.
  [CATransaction setDisableActions:YES];

显式动画CAAnimation

接口:

1、CAAction
2、CAMediaTiming

子类:

  • CAPropertyAnimation
    • CABasicAnimation
    • CAKeyframeAnimation
  • CAAnimationGroup
  • CATransition

混合View及Layer动画

<待补充>

你可能感兴趣的:(iOS开发高级进阶(8-10)-动画)