iOS 转场动画 WWDC2013 Session218笔记

唐巧博客
WWDC2013 Session218
Onecat博客

哪些转场可以被customized:

  1. Presentations and dismissals (模态的显示与消失)
  2. UITabBarController (切换)
  3. UINavigationController (Pop or Push)
  4. UICollectionViewController layout to lyout transitions

UIView Animation

UIView Animation

1
2
3
[UIView animationWithDuration:....animations:^{
....
}]

Spring Animation DampingRatio

1
2
3
4
5
6
7
+ (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

如果dampRatio = 1,则不会有振动,比1越小,振动越剧烈。

New KeyframeAnimation

iOS7 新加的方法可以替代 CAKeyframeAniamtion

1
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations
1
+ (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);

上述两个方法组合使用:

1
2
3
[UIView animateKeyframesWithDuration:1.3 delay:0 options:(UIViewKeyframeAnimationOptionAllowUserInteraction) animations:^{
[UIView addKeyframeWithRelative......];
} completion:nil];

这样就不用每次去通过Layer和CAKeyframeAniamtion来构造关键帧动画了。

Keyframe Animation Options:

1
2
3
4
5
UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10, // default
UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10,
UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10

曲线横轴表示时间,纵轴表示改变量的变化情况,比如我们修改的alpha、width之类的。

在CAKeyframeAnimation中的rotation可以直接使用view的transform来代替。

SnapShot

UIDynamics

Transitions:

1. delegate:

  1. <UINavigationControllerDelegate>
  2. <UITabBarControllerDelegate>
  3. <UIViewControllerTransitionDelegate>

2. Animation Controllers:

负责添加视图以及执行动画,conforms to<UIViewControllerAnimatedTransitioning>

3. Interaction Controllers:

手势驱动动画控制器实现的动画,
conforms to <UIViewControllerInteractiveTransitioning>

4. Transition Context:

conforms to <UIViewControllerContextTransitioning>

5. Transition Coordinator

在转场动画发生时执行其他的动画
conforms to <UIViewControllerTransitionCoordinator>

Protocols:

UIViewControllerContextTransitioning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 转场动画发生的容器。
- (UIView *)containerView;

// get controllers: use two keys:
// keys:
NSString *const UITransitionContextFromViewControllerKey;
NSString *const UITransitionContextToViewControllerKey;
- (nullable __kindof UIViewController *)viewControllerForKey:(NSString *)key;

// 获取一个controller的初始frame
- (CGRect)initialFrameForViewController:(UIViewController *)vc

// 获取最终frame
- (CGRect)finalFrameForViewController:(UIViewController *)vc

UIViewControllerAnimatedTransitioning 负责切换中的具体内容

1
2
3
4
5
// 切换时UIView的设置与动画在此实现
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

// 返回动画所需时间。
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

UIViewControllerTransitioningDelegate

1
2
3
4
5
6
7
8
9
10
// 
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
//
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed;
//
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
//
- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;
//
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0);

手势

UIViewControllerContextTransitioning

1
2
3
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
- (void)finishInteractiveTransition;
- (void)cancelInteractiveTransition;

Class: UIPercentDrivenInteractiveTransition

1
2
3
4
5
6
// 更新百分比,比如说根据手势滑动的距离
- (void)updateInteractiveTransition:(CGFloat)percentComplete;
// notify to the system that the transition has been canceled
- (void)cancelInteractiveTransition;
// notify to the system that the transition has finished
- (void)finishInteractiveTransition;

UIViewControllerInteractiveTransitioning

1
- (void)startInteractiveTransition:(id <UIViewControllerContextTransitioning>)transitionContext;