_ASTransitionContext.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // _ASTransitionContext.m
  3. // AsyncDisplayKit
  4. //
  5. // Created by Levi McCallum on 2/4/16.
  6. //
  7. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  8. // This source code is licensed under the BSD-style license found in the
  9. // LICENSE file in the root directory of this source tree. An additional grant
  10. // of patent rights can be found in the PATENTS file in the same directory.
  11. //
  12. #import <AsyncDisplayKit/_ASTransitionContext.h>
  13. #import <AsyncDisplayKit/ASDisplayNode.h>
  14. #import <AsyncDisplayKit/ASLayout.h>
  15. NSString * const ASTransitionContextFromLayoutKey = @"org.asyncdisplaykit.ASTransitionContextFromLayoutKey";
  16. NSString * const ASTransitionContextToLayoutKey = @"org.asyncdisplaykit.ASTransitionContextToLayoutKey";
  17. @interface _ASTransitionContext ()
  18. @property (weak, nonatomic) id<_ASTransitionContextLayoutDelegate> layoutDelegate;
  19. @property (weak, nonatomic) id<_ASTransitionContextCompletionDelegate> completionDelegate;
  20. @end
  21. @implementation _ASTransitionContext
  22. - (instancetype)initWithAnimation:(BOOL)animated
  23. layoutDelegate:(id<_ASTransitionContextLayoutDelegate>)layoutDelegate
  24. completionDelegate:(id<_ASTransitionContextCompletionDelegate>)completionDelegate
  25. {
  26. self = [super init];
  27. if (self) {
  28. _animated = animated;
  29. _layoutDelegate = layoutDelegate;
  30. _completionDelegate = completionDelegate;
  31. }
  32. return self;
  33. }
  34. #pragma mark - ASContextTransitioning Protocol Implementation
  35. - (ASLayout *)layoutForKey:(NSString *)key
  36. {
  37. return [_layoutDelegate transitionContext:self layoutForKey:key];
  38. }
  39. - (ASSizeRange)constrainedSizeForKey:(NSString *)key
  40. {
  41. return [_layoutDelegate transitionContext:self constrainedSizeForKey:key];
  42. }
  43. - (CGRect)initialFrameForNode:(ASDisplayNode *)node
  44. {
  45. for (ASDisplayNode *subnode in [_layoutDelegate currentSubnodesWithTransitionContext:self]) {
  46. if (node == subnode) {
  47. return node.frame;
  48. }
  49. }
  50. return CGRectZero;
  51. }
  52. - (CGRect)finalFrameForNode:(ASDisplayNode *)node
  53. {
  54. for (ASLayout *layout in [self layoutForKey:ASTransitionContextToLayoutKey].sublayouts) {
  55. if (layout.layoutElement == node) {
  56. return [layout frame];
  57. }
  58. }
  59. return CGRectZero;
  60. }
  61. - (NSArray<ASDisplayNode *> *)subnodesForKey:(NSString *)key
  62. {
  63. NSMutableArray<ASDisplayNode *> *subnodes = [NSMutableArray array];
  64. for (ASLayout *sublayout in [self layoutForKey:key].sublayouts) {
  65. [subnodes addObject:(ASDisplayNode *)sublayout.layoutElement];
  66. }
  67. return subnodes;
  68. }
  69. - (NSArray<ASDisplayNode *> *)insertedSubnodes
  70. {
  71. return [_layoutDelegate insertedSubnodesWithTransitionContext:self];
  72. }
  73. - (NSArray<ASDisplayNode *> *)removedSubnodes
  74. {
  75. return [_layoutDelegate removedSubnodesWithTransitionContext:self];
  76. }
  77. - (void)completeTransition:(BOOL)didComplete
  78. {
  79. [_completionDelegate transitionContext:self didComplete:didComplete];
  80. }
  81. @end
  82. @interface _ASAnimatedTransitionContext ()
  83. @property (nonatomic, strong, readwrite) ASDisplayNode *node;
  84. @property (nonatomic, assign, readwrite) CGFloat alpha;
  85. @end
  86. @implementation _ASAnimatedTransitionContext
  87. + (instancetype)contextForNode:(ASDisplayNode *)node alpha:(CGFloat)alpha
  88. {
  89. _ASAnimatedTransitionContext *context = [[_ASAnimatedTransitionContext alloc] init];
  90. context.node = node;
  91. context.alpha = alpha;
  92. return context;
  93. }
  94. @end