Last active
November 5, 2015 16:05
-
-
Save lucabartoletti/a5f103cb74a3edb178d6 to your computer and use it in GitHub Desktop.
Sample MapView to replicate a bug (http://stackoverflow.com/questions/33546930/mkmapview-center-not-correct)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import <MapKit/MapKit.h> | |
static CGFloat kCMStartY = -150; | |
@interface ViewController () | |
@property (nonatomic, strong, nullable) MKMapView *mapView; | |
@property (nonatomic, strong, nullable) UIView *mapViewContainer; | |
@property (nonatomic, strong, nullable) UIView *squareView; | |
@property (nonatomic, strong, nullable) UIView *overlayView; | |
@property (nonatomic, assign) CGFloat currentY; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.mapView = [MKMapView new]; | |
self.squareView = [UIView new]; | |
self.squareView.backgroundColor = [UIColor orangeColor]; | |
self.currentY = kCMStartY; | |
self.mapViewContainer = [UIView new]; | |
[self.view addSubview:self.mapViewContainer]; | |
[self.mapViewContainer addSubview:self.mapView]; | |
[self.mapViewContainer addSubview:self.squareView]; | |
self.overlayView = [UIView new]; | |
[self.view addSubview:self.overlayView]; | |
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRegognized:)]; | |
[self.overlayView addGestureRecognizer:panGestureRecognizer]; | |
[NSTimer scheduledTimerWithTimeInterval:0.5 | |
target:self | |
selector:@selector(timerFired) | |
userInfo:nil | |
repeats:YES]; | |
} | |
- (void)viewDidLayoutSubviews { | |
self.overlayView.frame = self.view.bounds; | |
self.mapViewContainer.frame = CGRectMake(0, self.currentY, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)); | |
if (!CGRectEqualToRect(self.mapViewContainer.bounds, self.mapView.bounds)) { | |
self.mapView.bounds = CGRectMake(0, | |
0, | |
CGRectGetWidth(self.mapViewContainer.bounds), | |
CGRectGetHeight(self.mapViewContainer.bounds)); | |
self.mapView.center = CGPointMake(CGRectGetMidX(self.mapViewContainer.bounds), | |
CGRectGetMidY(self.mapViewContainer.bounds)); | |
} | |
self.squareView.bounds = CGRectMake(0, 0, 22.0f, 22.0f); | |
self.squareView.center = CGPointMake(CGRectGetMidX(self.mapViewContainer.bounds), | |
CGRectGetMidY(self.mapViewContainer.bounds)); | |
} | |
- (void)timerFired { | |
[self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(51.501367, -0.141744), MKCoordinateSpanMake(0.015, 0.015))]; | |
} | |
- (void)panGestureRegognized:(UIPanGestureRecognizer *)panGestureRecognizer { | |
CGPoint translation = [panGestureRecognizer translationInView:self.overlayView]; | |
CGFloat y = kCMStartY; | |
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || | |
panGestureRecognizer.state == UIGestureRecognizerStateChanged) { | |
y = translation.y; | |
} | |
self.currentY = y; | |
[self.view setNeedsLayout]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment