iPhone東京へ

地図機能を使って、世界地図から東京にアニメーションさせるiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController ()

@property (nonatomic, weak) MKMapView *mapView;

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [self createWorldMap];

    [self createWhereTokyoButton];

    [self createBackToWorldMapButton];

}

– (void)createWorldMap

{

    MKMapView *map = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.bounds), CGRectGetMaxY(self.view.bounds))];

    [self.view addSubview:map];

    

    MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);

    map.region = worldRegion;

    self.mapView = map;

}

– (void)createWhereTokyoButton

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(250, 280, 100, 50);

    btn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

    btn.layer.cornerRadius = 5;

    [btn setTitle:@”→ Tokyo” forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];

    [self.view addSubview:btn];

    

    [btn addTarget:self action:@selector(tokyo) forControlEvents:UIControlEventTouchUpInside];

}

– (void)createBackToWorldMapButton

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(360, 280, 100, 50);

    btn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

    btn.layer.cornerRadius = 5;

    [btn setTitle:@”→ World” forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];

    

    [self.view addSubview:btn];

    [btn addTarget:self action:@selector(world) forControlEvents:UIControlEventTouchUpInside];

}

– (void)tokyo

{

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:@”Tokyo” completionHandler:^(NSArray *placemarks, NSError *error) {

        if (placemarks.count) {

            CLPlacemark *placemark = placemarks[0];

            float spanX = 20.0;

            float spanY = 20.0;

            

            MKCoordinateRegion region;

            region.center.latitude = placemark.location.coordinate.latitude;

            region.center.longitude = placemark.location.coordinate.longitude;

            region.span = MKCoordinateSpanMake(spanX, spanY);

            [self.mapView setRegion:region animated:YES];

            [self frameAnimation];

        }

    }];

}

– (void)world

{

    MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);

    [self.mapView setRegion:worldRegion animated:YES];

}

– (void)frameAnimation

{

    for (int i=0; i<5; i++) {

        UIView *v = [[UIView alloc] initWithFrame:self.view.bounds];

        v.layer.borderWidth = 6;

        v.layer.borderColor = [[UIColor greenColor] colorWithAlphaComponent:0.5].CGColor;

        [self.view addSubview:v];

        

        [UIView animateWithDuration:0.5 delay:2 + i*0.3 options:0 animations:^{

            v.frame = CGRectMake(CGRectGetMidX(self.view.bounds) – 15, CGRectGetMidY(self.view.bounds), 30, 10);

            v.transform = CGAffineTransformMakeRotation(M_PI);

        } completion:^(BOOL finished) {

            [v removeFromSuperview];

        }];

    }

}

@end