MASExampleAnimatedView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // MASExampleAnimatedView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Jonas Budelmann on 22/07/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleAnimatedView.h"
  9. @interface MASExampleAnimatedView ()
  10. @property (nonatomic, strong) NSMutableArray *animatableConstraints;
  11. @property (nonatomic, assign) int padding;
  12. @property (nonatomic, assign) BOOL animating;
  13. @end
  14. @implementation MASExampleAnimatedView
  15. - (id)init {
  16. self = [super init];
  17. if (!self) return nil;
  18. UIView *view1 = UIView.new;
  19. view1.backgroundColor = UIColor.greenColor;
  20. view1.layer.borderColor = UIColor.blackColor.CGColor;
  21. view1.layer.borderWidth = 2;
  22. [self addSubview:view1];
  23. UIView *view2 = UIView.new;
  24. view2.backgroundColor = UIColor.redColor;
  25. view2.layer.borderColor = UIColor.blackColor.CGColor;
  26. view2.layer.borderWidth = 2;
  27. [self addSubview:view2];
  28. UIView *view3 = UIView.new;
  29. view3.backgroundColor = UIColor.blueColor;
  30. view3.layer.borderColor = UIColor.blackColor.CGColor;
  31. view3.layer.borderWidth = 2;
  32. [self addSubview:view3];
  33. UIView *superview = self;
  34. int padding = self.padding = 10;
  35. UIEdgeInsets paddingInsets = UIEdgeInsetsMake(self.padding, self.padding, self.padding, self.padding);
  36. self.animatableConstraints = NSMutableArray.new;
  37. [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
  38. [self.animatableConstraints addObjectsFromArray:@[
  39. make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
  40. make.bottom.equalTo(view3.mas_top).offset(-padding),
  41. ]];
  42. make.size.equalTo(view2);
  43. make.height.equalTo(view3.mas_height);
  44. }];
  45. [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
  46. [self.animatableConstraints addObjectsFromArray:@[
  47. make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
  48. make.left.equalTo(view1.mas_right).offset(padding),
  49. make.bottom.equalTo(view3.mas_top).offset(-padding),
  50. ]];
  51. make.size.equalTo(view1);
  52. make.height.equalTo(view3.mas_height);
  53. }];
  54. [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
  55. [self.animatableConstraints addObjectsFromArray:@[
  56. make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
  57. ]];
  58. make.height.equalTo(view1.mas_height);
  59. make.height.equalTo(view2.mas_height);
  60. }];
  61. return self;
  62. }
  63. - (void)didMoveToWindow {
  64. [self layoutIfNeeded];
  65. if (self.window) {
  66. self.animating = YES;
  67. [self animateWithInvertedInsets:NO];
  68. }
  69. }
  70. - (void)willMoveToWindow:(UIWindow *)newWindow {
  71. self.animating = newWindow != nil;
  72. }
  73. - (void)animateWithInvertedInsets:(BOOL)invertedInsets {
  74. if (!self.animating) return;
  75. int padding = invertedInsets ? 100 : self.padding;
  76. UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
  77. for (MASConstraint *constraint in self.animatableConstraints) {
  78. constraint.insets = paddingInsets;
  79. }
  80. [UIView animateWithDuration:1 animations:^{
  81. [self layoutIfNeeded];
  82. } completion:^(BOOL finished) {
  83. //repeat!
  84. [self animateWithInvertedInsets:!invertedInsets];
  85. }];
  86. }
  87. @end