DetailDisclosureView.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "DetailDisclosureView.h"
  21. #import "RectUtil.h"
  22. #import "Animations.h"
  23. #define GESTURE_SWIPE_MIN_SPEED 150.0
  24. #define GESTURE_SWIPE_MIN_POINTS 50.0
  25. #define GESTURE_SWIPE_MAX_DURATION 0.3
  26. #define BOUNCE_OFFSET 80.0
  27. @interface DetailDisclosureView ()
  28. @property CGFloat currentXPos;
  29. @property BOOL showDetails;
  30. @property UIView *dragIndicator;
  31. @end
  32. @implementation DetailDisclosureView
  33. @synthesize ddDetailView = _ddDetailView, ddMainView = _ddMainView;
  34. - (void)awakeFromNib
  35. {
  36. [super awakeFromNib];
  37. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  38. [self addGestureRecognizer:pan];
  39. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
  40. [self addGestureRecognizer:tap];
  41. self.bounceOffset = BOUNCE_OFFSET;
  42. self.clipsToBounds = YES;
  43. }
  44. - (void)moveViewsWithVelocity:(CGPoint) velocity toXOffset:(CGFloat) xOffset
  45. {
  46. CGFloat points2move = fabs(self.ddMainView.frame.origin.x - xOffset);
  47. CGFloat duration = points2move / fabs(velocity.x);
  48. duration = MIN(duration, GESTURE_SWIPE_MAX_DURATION);
  49. UIViewAnimationOptions options = UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState;
  50. [UIView animateWithDuration:duration delay:0.0 options:options animations:^{
  51. [self positionViewsAtX:xOffset];
  52. } completion:^(BOOL finished) {
  53. }];
  54. }
  55. - (void)positionViewsAtX:(CGFloat)x {
  56. _currentXPos = x;
  57. self.ddMainView.frame = [RectUtil setXPositionOf:self.ddMainView.frame x: x];
  58. self.ddDetailView.frame = [RectUtil setXPositionOf:self.ddDetailView.frame x: x + CGRectGetWidth(self.ddMainView.frame)];
  59. }
  60. + (CGFloat)limit:(CGFloat)value toMin:(CGFloat)min max:(CGFloat)max {
  61. CGFloat result = fminf(value, max);
  62. result = fmaxf(result, min);
  63. return result;
  64. }
  65. - (void)moveViewsByX:(CGFloat)diffX
  66. {
  67. CGFloat newX = _currentXPos + diffX;
  68. if (newX <= - CGRectGetWidth(_ddDetailView.frame) || newX >= 0.0) {
  69. return;
  70. }
  71. [self positionViewsAtX:newX];
  72. }
  73. #pragma mark - Animations
  74. - (void)bounceDetailView {
  75. [self bringSubviewToFront:_ddDetailView];
  76. CGRect startRect = [RectUtil setXPositionOf:_ddDetailView.frame x: (self.frame.size.width - _bounceOffset)];
  77. [Animations bounceToRect:startRect view:_ddDetailView endAction:nil];
  78. }
  79. - (void)toggleDetailView {
  80. CGFloat xOffset;
  81. if (_showDetails) {
  82. xOffset = 0.0;
  83. } else {
  84. xOffset = - CGRectGetWidth(_ddDetailView.frame);
  85. }
  86. _showDetails = !_showDetails;
  87. CGPoint velocity = CGPointMake(GESTURE_SWIPE_MIN_SPEED, 0.0);
  88. [self moveViewsWithVelocity:velocity toXOffset:xOffset];
  89. }
  90. #pragma mark - UIPanGestureRecognizer
  91. - (void)tap:(UIPanGestureRecognizer *)recognizer {
  92. [self toggleDetailView];
  93. }
  94. - (void)pan:(UIPanGestureRecognizer *)recognizer {
  95. if (recognizer.state == UIGestureRecognizerStateBegan) {
  96. } else if (recognizer.state == UIGestureRecognizerStateChanged) {
  97. CGPoint transformed = [recognizer translationInView:self];
  98. [self moveViewsByX:transformed.x];
  99. [recognizer setTranslation:CGPointZero inView:self];
  100. } else if (recognizer.state == UIGestureRecognizerStateEnded) {
  101. CGFloat xOffset;
  102. if ([self movedLeftAcrossTrigger] && [self checkSpeedTrigger:recognizer]) {
  103. xOffset = 0.0 - CGRectGetWidth(_ddDetailView.frame);
  104. _showDetails = YES;
  105. } else if ([self movedRightAcrossTrigger] && [self checkSpeedTrigger:recognizer]) {
  106. xOffset = 0.0;
  107. _showDetails = NO;
  108. } else {
  109. if (_showDetails) {
  110. xOffset = - CGRectGetWidth(_ddDetailView.frame);
  111. } else {
  112. xOffset = 0.0;
  113. }
  114. }
  115. CGPoint velocity = [recognizer velocityInView:self];
  116. [self moveViewsWithVelocity:velocity toXOffset:xOffset];
  117. }
  118. }
  119. - (BOOL)checkSpeedTrigger:(UIPanGestureRecognizer *)recognizer
  120. {
  121. CGPoint velocity = [recognizer velocityInView:self];
  122. return (fabs(velocity.x) > GESTURE_SWIPE_MIN_SPEED);
  123. }
  124. - (BOOL)movedRightAcrossTrigger
  125. {
  126. return _showDetails && _ddMainView.frame.origin.x + CGRectGetWidth(_ddDetailView.frame) > GESTURE_SWIPE_MIN_POINTS;
  127. }
  128. - (BOOL)movedLeftAcrossTrigger
  129. {
  130. return _showDetails == NO && _ddMainView.frame.origin.x < -GESTURE_SWIPE_MIN_POINTS;
  131. }
  132. @end