Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMSideButtonsView.mm « SideButtons « MapViewControls « CustomViews « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cec34392b199a1b242e4630809ed3d6c2b17742c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#import "MWMSideButtonsView.h"
#import "MWMCommon.h"
#import "MWMBottomMenuViewController.h"
#import "MWMButton.h"
#import "MWMMapViewControlsCommon.h"

#include "base/math.hpp"

namespace
{
CGFloat const kLocationButtonSpacingMax = 52;
CGFloat const kLocationButtonSpacingMin = 8;
CGFloat const kButtonsTopOffset = 6;
CGFloat const kButtonsBottomOffset = 6;
}  // namespace

@interface MWMSideButtonsView ()

@property(weak, nonatomic) IBOutlet MWMButton * zoomIn;
@property(weak, nonatomic) IBOutlet MWMButton * zoomOut;
@property(weak, nonatomic) IBOutlet MWMButton * location;

@property(nonatomic) CGRect availableArea;

@end

@implementation MWMSideButtonsView

- (void)awakeFromNib
{
  [super awakeFromNib];
  self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

- (void)layoutSubviews
{
  CGFloat spacing = self.availableHeight - self.zoomOut.maxY - self.location.height;
  spacing = my::clamp(spacing, kLocationButtonSpacingMin, kLocationButtonSpacingMax);

  self.location.minY = self.zoomOut.maxY + spacing;
  self.bounds = {{}, {self.zoomOut.width, self.location.maxY}};
  if (self.zoomHidden)
    self.height = self.location.height;
  self.location.maxY = self.height;

  [self layoutXPosition:self.hidden];
  [self animate];
  [super layoutSubviews];
}

- (void)layoutXPosition:(BOOL)hidden
{
  if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft)
  {
    if (hidden)
      self.maxX = 0;
    else
      self.minX = kViewControlsOffsetToBounds;
  }
  else
  {
    if (hidden)
      self.minX = self.superview.width;
    else
      self.maxX = self.superview.width - kViewControlsOffsetToBounds;
  }
}

- (void)layoutYPosition
{
  CGFloat const centerShift = (self.height - self.zoomIn.midY - self.zoomOut.midY) / 2;
  self.midY = centerShift + self.superview.height / 2;
  if (self.maxY > self.bottomBound)
    self.maxY = self.bottomBound;
}

- (void)fadeZoomButtonsShow:(BOOL)show
{
  CGFloat const alpha = show ? 1.0 : 0.0;
  [UIView animateWithDuration:framesDuration(kMenuViewHideFramesCount)
                   animations:^{
                     self.zoomIn.alpha = alpha;
                     self.zoomOut.alpha = alpha;
                   }];
}

- (void)fadeLocationButtonShow:(BOOL)show
{
  [UIView animateWithDuration:framesDuration(kMenuViewHideFramesCount)
                   animations:^{
                     self.location.alpha = show ? 1.0 : 0.0;
                   }];
}

- (void)animate
{
  dispatch_async(dispatch_get_main_queue(), ^{
    [self layoutYPosition];

    auto const spaceLeft = self.availableHeight;
    BOOL const isZoomHidden = self.zoomIn.alpha == 0.0;
    BOOL const willZoomHide = (self.location.maxY > spaceLeft);
    if (willZoomHide)
    {
      if (!isZoomHidden)
        [self fadeZoomButtonsShow:NO];
    }
    else
    {
      if (isZoomHidden)
        [self fadeZoomButtonsShow:YES];
    }
    BOOL const isLocationHidden = self.location.alpha == 0.0;
    BOOL const willLocationHide = (self.location.height > spaceLeft);
    if (willLocationHide)
    {
      if (!isLocationHidden)
        [self fadeLocationButtonShow:NO];
    }
    else
    {
      if (isLocationHidden)
        [self fadeLocationButtonShow:YES];
    }
    [self layoutIfNeeded];
  });
}

#pragma mark - Properties

- (void)setZoomHidden:(BOOL)zoomHidden
{
  _zoomHidden = zoomHidden;
  CGFloat const minX = zoomHidden ? self.width + kViewControlsOffsetToBounds : 0.0;
  self.zoomIn.minX = minX;
  self.zoomOut.minX = minX;
  [self setNeedsLayout];
}

- (void)setHidden:(BOOL)hidden animated:(BOOL)animated
{
  if (animated)
  {
    if (self.hidden == hidden)
      return;
    if (!hidden)
      self.hidden = NO;
    [UIView animateWithDuration:framesDuration(kMenuViewHideFramesCount)
        animations:^{
          [self layoutXPosition:hidden];
        }
        completion:^(BOOL finished) {
          if (hidden)
            self.hidden = YES;
        }];
  }
  else
  {
    self.hidden = hidden;
  }
}

- (void)updateAvailableArea:(CGRect)frame
{
  if (CGRectEqualToRect(self.availableArea, frame))
    return;
  self.availableArea = frame;
  [self setNeedsLayout];
}

- (CGFloat)availableHeight
{
  return self.availableArea.size.height - kButtonsTopOffset - kButtonsBottomOffset;
}

- (CGFloat)topBound { return self.availableArea.origin.y + kButtonsTopOffset; }
- (CGFloat)bottomBound
{
  auto const area = self.availableArea;
  return area.origin.y + area.size.height - kButtonsBottomOffset;
}

@end