Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios] Disable/enable related gesture recognizer when disabling/enabli…
Browse files Browse the repository at this point in the history
…ng user interactivity. Fixes #7217

Added an example behavior of MGLMapView as a subview of UIScrollView in
iOS app.
  • Loading branch information
davidchiles authored and boundsj committed Apr 6, 2017
1 parent e05a6bb commit 2b22d7f
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 21 deletions.
13 changes: 13 additions & 0 deletions platform/ios/app/MBXEmbededMapViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// MBXEmbededMapViewController.h
// ios
//
// Created by David Chiles on 3/7/17.
// Copyright © 2017 Mapbox. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MBXEmbededMapViewController : UIViewController

@end
153 changes: 153 additions & 0 deletions platform/ios/app/MBXEmbededMapViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//
// MBXEmbededMapViewController.m
// ios
//
// Created by David Chiles on 3/7/17.
// Copyright © 2017 Mapbox. All rights reserved.
//

#import "MBXEmbededMapViewController.h"
#import <Mapbox/Mapbox.h>

typedef NS_ENUM(NSInteger, MBXEmbeddedControl) {
MBXEmbeddedControlZoom = 0,
MBXEmbeddedControlScroll,
MBXEmbeddedControlRotation,
MBXEmbeddedControlPitch
};


@interface MBXEmbededMapViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) MGLMapView *mapView;

@end

@implementation MBXEmbededMapViewController

- (void)viewDidLoad {
[super viewDidLoad];

UIRotationGestureRecognizer *gestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.delegate = self;
[self.view addSubview:scrollView];
[scrollView addGestureRecognizer:gestureRecognizer];

CGRect mapRect = CGRectMake(0, 0, CGRectGetWidth(scrollView.frame) * 2, CGRectGetHeight(scrollView.frame) * 2);
self.mapView = [[MGLMapView alloc] initWithFrame:mapRect];
[scrollView addSubview:self.mapView];
scrollView.contentSize = CGSizeMake(CGRectGetWidth(mapRect), CGRectGetHeight(mapRect));

CGFloat widthScale = self.view.bounds.size.width / CGRectGetWidth(mapRect);
CGFloat heightScale = self.view.bounds.size.height / CGRectGetHeight(mapRect);
CGFloat minScale = MIN(widthScale, heightScale);

scrollView.maximumZoomScale = minScale*10;
scrollView.minimumZoomScale = minScale;

//Create list of all the possible control titles
NSMutableArray <UIView*>*items = [[NSMutableArray alloc] init];
for(NSInteger index =0; index < 4; index++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:12];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = [[self class] titleForControl:index];
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectZero];
switchControl.tag = index;
[switchControl addTarget:self action:@selector(didSwitch:) forControlEvents:UIControlEventValueChanged];
switchControl.translatesAutoresizingMaskIntoConstraints = NO;
switchControl.on = [self statusForControl:index];
[view addSubview:label];
[view addSubview:switchControl];
[items addObject:view];

[view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:switchControl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:switchControl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

}

UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:items];
stackView.alignment = UIStackViewAlignmentFill;
stackView.distribution = UIStackViewDistributionFillEqually;
stackView.frame = CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.frame), 65);

[self.view addSubview:stackView];
}

- (void)didSwitch:(UISwitch *)sw {
[self switchContorl:sw.tag];
if (sw.tag == MBXEmbeddedControlRotation) {

}
}

- (void)rotation:(UIRotationGestureRecognizer *)rotationGesture
{
self.mapView.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
}

- (void)switchContorl:(MBXEmbeddedControl) control {
switch (control) {
case MBXEmbeddedControlZoom:
self.mapView.zoomEnabled = !self.mapView.zoomEnabled;
break;
case MBXEmbeddedControlScroll:
self.mapView.scrollEnabled = !self.mapView.scrollEnabled;
break;
case MBXEmbeddedControlRotation:
self.mapView.rotateEnabled = !self.mapView.rotateEnabled;
break;
case MBXEmbeddedControlPitch:
self.mapView.pitchEnabled = !self.mapView.pitchEnabled;
break;
}
}

- (BOOL)statusForControl:(MBXEmbeddedControl) control {
switch (control) {
case MBXEmbeddedControlZoom:
return self.mapView.zoomEnabled;
case MBXEmbeddedControlScroll:
return self.mapView.scrollEnabled;
case MBXEmbeddedControlRotation:
return self.mapView.rotateEnabled;
case MBXEmbeddedControlPitch:
return self.mapView.pitchEnabled;
}
}

#pragma mark UIScrollViewDelegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.mapView;
}

#pragma mark Class method


+ (NSString *)titleForControl:(MBXEmbeddedControl) control {
switch (control) {
case MBXEmbeddedControlZoom:
return @"Zoom Enabled";
case MBXEmbeddedControlScroll:
return @"Scroll Enabled";
break;
case MBXEmbeddedControlRotation:
return @"Rotation Enabled";
break;
case MBXEmbeddedControlPitch:
return @"Pitch Enabled";
break;
}
}

@end
14 changes: 13 additions & 1 deletion platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import "MBXOfflinePacksTableViewController.h"
#import "MBXAnnotationView.h"
#import "MBXUserLocationAnnotationView.h"
#import "MBXEmbededMapViewController.h"

#import <Mapbox/Mapbox.h>

Expand Down Expand Up @@ -79,8 +80,10 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
MBXSettingsMiscellaneousWorldTour,
MBXSettingsMiscellaneousCustomUserDot,
MBXSettingsMiscellaneousShowZoomLevel,
MBXSettingsMiscellaneousScrollView,
MBXSettingsMiscellaneousPrintLogFile,
MBXSettingsMiscellaneousDeleteLogFile,
MBXSettingsMiscellaneousDeleteLogFile

};

@interface MBXDroppedPinAnnotation : MGLPointAnnotation
Expand Down Expand Up @@ -350,6 +353,7 @@ - (void)dismissSettings:(__unused id)sender
@"Start World Tour",
[NSString stringWithFormat:@"%@ Custom User Dot", (_customUserLocationAnnnotationEnabled ? @"Disable" : @"Enable")],
[NSString stringWithFormat:@"%@ Zoom Level", (_showZoomLevelEnabled ? @"Hide" :@"Show")],
@"Embeded Map View"
]];

if (self.debugLoggingEnabled)
Expand Down Expand Up @@ -615,6 +619,14 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
self.reuseQueueStatsEnabled = NO;
break;
}
case MBXSettingsMiscellaneousScrollView:
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MBXEmbededMapViewController *embeddedMapViewController = (MBXEmbededMapViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MBXEmbededMapViewController"];
[self.navigationController pushViewController:embeddedMapViewController animated:YES];

break;
}
default:
NSAssert(NO, @"All miscellaneous setting rows should be implemented");
break;
Expand Down
43 changes: 33 additions & 10 deletions platform/ios/app/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="PSe-Ot-7Ff">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="PSe-Ot-7Ff">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Navigation items with more than one left or right bar item" minToolsVersion="7.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -21,6 +24,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kNe-zV-9ha" customClass="MGLMapView">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<gestureRecognizers/>
<connections>
Expand All @@ -29,6 +33,7 @@
</connections>
</view>
<label opaque="NO" userInteractionEnabled="NO" alpha="0.69999999999999996" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="58y-pX-YyB">
<rect key="frame" x="179" y="626" width="180" height="21"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="width" constant="180" id="OL2-l5-I2f"/>
Expand Down Expand Up @@ -111,21 +116,21 @@
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Inactive" editingAccessoryType="detailDisclosureButton" textLabel="JtH-Ce-MI5" detailTextLabel="tTJ-jv-U9v" style="IBUITableViewCellStyleSubtitle" id="fGu-Ys-Eh1">
<rect key="frame" x="0.0" y="92" width="375" height="44"/>
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="fGu-Ys-Eh1" id="sUf-bc-8xG">
<frame key="frameInset" width="375" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="My Inactive Offline Pack" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="JtH-Ce-MI5">
<frame key="frameInset" minX="15" minY="6" width="174.5" height="19.5"/>
<rect key="frame" x="15" y="6" width="174.5" height="19.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="456 resources (789 MB)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="tTJ-jv-U9v">
<frame key="frameInset" minX="15" minY="25.5" width="128" height="13.5"/>
<rect key="frame" x="15" y="25.5" width="128" height="13.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -135,21 +140,21 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Active" editingAccessoryType="detailDisclosureButton" textLabel="9ZK-gS-wJ4" detailTextLabel="0xK-p8-Mmh" style="IBUITableViewCellStyleSubtitle" id="mKB-tz-Zfl">
<rect key="frame" x="0.0" y="136" width="375" height="44"/>
<rect key="frame" x="0.0" y="72" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mKB-tz-Zfl" id="nS3-aU-nBr">
<frame key="frameInset" width="375" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="My Active Offline Pack" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="9ZK-gS-wJ4">
<frame key="frameInset" minX="15" minY="6" width="163" height="19.5"/>
<rect key="frame" x="15" y="6" width="163" height="19.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Downloading 123 of 456 resources… (789 MB downloaded)" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="0xK-p8-Mmh">
<frame key="frameInset" minX="15" minY="25.5" width="310.5" height="13.5"/>
<rect key="frame" x="15" y="25.5" width="310.5" height="13.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down Expand Up @@ -198,6 +203,24 @@
</objects>
<point key="canvasLocation" x="554" y="350"/>
</scene>
<!--Embeded Map View Controller-->
<scene sceneID="dGM-LS-4VE">
<objects>
<viewController storyboardIdentifier="MBXEmbededMapViewController" id="Tsi-Cv-L66" customClass="MBXEmbededMapViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="xne-oT-1Cv"/>
<viewControllerLayoutGuide type="bottom" id="bxa-Bm-Qun"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="vKr-y9-AZt">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="k2c-Gr-mpl" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="593" y="1084"/>
</scene>
</scenes>
<resources>
<image name="TrackingLocationOffMask.png" width="23" height="23"/>
Expand Down
Loading

0 comments on commit 2b22d7f

Please sign in to comment.