PaddingBehaviorViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2017 Google Inc. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GoogleMapsDemos/Samples/PaddingBehaviorViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. static CLLocationCoordinate2D kPanoramaNear = {40.761388, -73.978133};
  18. @interface PaddingBehaviorViewController () <GMSMapViewDelegate>
  19. @end
  20. @implementation PaddingBehaviorViewController {
  21. GMSMapView *_mapView;
  22. GMSPanoramaView *_panoramaView;
  23. UILabel *_statusLabel;
  24. UIButton *_changeBehaviorButton;
  25. UIButton *_toggleFrameButton;
  26. UIBarButtonItem *_toggleViewButton;
  27. BOOL _hasShrunk;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. GMSCameraPosition *camera =
  32. [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:6];
  33. _mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
  34. _mapView.padding = UIEdgeInsetsMake(0, 20, 40, 60);
  35. _mapView.delegate = self;
  36. _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  37. [self.view addSubview:_mapView];
  38. // Add status label.
  39. _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 0, 30)];
  40. _statusLabel.textColor = [UIColor brownColor];
  41. _statusLabel.textAlignment = NSTextAlignmentLeft;
  42. _statusLabel.text = @"Behavior: Always";
  43. [_statusLabel sizeToFit];
  44. // Add behavior modifier button.
  45. _changeBehaviorButton = [UIButton buttonWithType:UIButtonTypeSystem];
  46. _changeBehaviorButton.frame = CGRectMake(30, 30, 0, 0);
  47. [_changeBehaviorButton setTitle:@"Next Behavior" forState:UIControlStateNormal];
  48. [_changeBehaviorButton sizeToFit];
  49. [_changeBehaviorButton addTarget:self
  50. action:@selector(nextBehavior)
  51. forControlEvents:UIControlEventTouchUpInside];
  52. // Add frame animation button.
  53. _toggleViewButton = [[UIBarButtonItem alloc] initWithTitle:@"Toggle View"
  54. style:UIBarButtonItemStylePlain
  55. target:self
  56. action:@selector(toggleViewType)];
  57. self.navigationItem.rightBarButtonItem = _toggleViewButton;
  58. // Add change view type button.
  59. _toggleFrameButton = [UIButton buttonWithType:UIButtonTypeSystem];
  60. _toggleFrameButton.frame = CGRectMake(30, 60, 0, 0);
  61. [_toggleFrameButton setTitle:@"Animate Frame" forState:UIControlStateNormal];
  62. [_toggleFrameButton sizeToFit];
  63. [_toggleFrameButton addTarget:self
  64. action:@selector(toggleFrame)
  65. forControlEvents:UIControlEventTouchUpInside];
  66. [_mapView addSubview:_statusLabel];
  67. [_mapView addSubview:_changeBehaviorButton];
  68. [_mapView addSubview:_toggleFrameButton];
  69. _hasShrunk = NO;
  70. // Pre-load PanoramaView
  71. _panoramaView = [GMSPanoramaView panoramaWithFrame:self.view.bounds nearCoordinate:kPanoramaNear];
  72. _panoramaView.autoresizingMask =
  73. UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  74. }
  75. #pragma mark - Button Click Handlers
  76. - (void)toggleFrame {
  77. CGSize size = self.view.bounds.size;
  78. CGPoint point = self.view.bounds.origin;
  79. [UIView animateWithDuration:2.0
  80. animations:^{
  81. if (self->_hasShrunk) {
  82. self->_mapView.frame = self.view.bounds;
  83. self->_panoramaView.frame = self->_mapView.frame;
  84. } else {
  85. self->_mapView.frame =
  86. CGRectMake(point.x, point.y, size.width / 2, size.height / 2);
  87. self->_panoramaView.frame = self->_mapView.frame;
  88. }
  89. self->_hasShrunk = !self->_hasShrunk;
  90. [self.view setNeedsLayout];
  91. [self.view layoutIfNeeded];
  92. }];
  93. }
  94. - (void)toggleViewType {
  95. if ([self.view.subviews containsObject:_mapView]) {
  96. [_mapView removeFromSuperview];
  97. [self.view addSubview:_panoramaView];
  98. [_panoramaView addSubview:_toggleFrameButton];
  99. } else {
  100. [_panoramaView removeFromSuperview];
  101. [self.view addSubview:_mapView];
  102. [_mapView addSubview:_toggleFrameButton];
  103. }
  104. }
  105. - (void)nextBehavior {
  106. switch (_mapView.paddingAdjustmentBehavior) {
  107. case kGMSMapViewPaddingAdjustmentBehaviorAlways:
  108. _mapView.paddingAdjustmentBehavior = kGMSMapViewPaddingAdjustmentBehaviorAutomatic;
  109. _statusLabel.text = @"Behavior: Automatic";
  110. break;
  111. case kGMSMapViewPaddingAdjustmentBehaviorAutomatic:
  112. _mapView.paddingAdjustmentBehavior = kGMSMapViewPaddingAdjustmentBehaviorNever;
  113. _statusLabel.text = @"Behavior: Never";
  114. break;
  115. case kGMSMapViewPaddingAdjustmentBehaviorNever:
  116. _mapView.paddingAdjustmentBehavior = kGMSMapViewPaddingAdjustmentBehaviorAlways;
  117. _statusLabel.text = @"Behavior: Always";
  118. break;
  119. }
  120. }
  121. @end