iPhoneナスカのハチドリ

地図にナスカのハチドリを表示するiPhoneアプリのサンプルコードを描いてみます。

ボタン画像

#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController ()

@property (nonatomic, weak) MKMapView *map;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createMapView];

    [self createButton];

}

– (void)createMapView

{

    MKMapView *mv = [[MKMapView alloc] initWithFrame:self.view.bounds];

    mv.mapType = MKMapTypeSatellite;

    mv.userInteractionEnabled = NO;

    [self.view addSubview:mv];

    self.map = mv;

}

– (void)createButton

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setImage:[UIImage imageNamed:@”hummingBird”] forState:UIControlStateNormal];

    button.frame = CGRectMake(0, 0, 80, 80);

    button.center = CGPointMake(280, CGRectGetMaxY(self.view.frame) – 40);

    [self.view addSubview:button];

    

    [button addTarget:self action:@selector(showHummingBird) forControlEvents:UIControlEventTouchUpInside];

}

– (void)moveAtCoordinate:(CLLocationCoordinate2D)coordinate

{

    MKCoordinateRegion region;

    region.center.latitude = coordinate.latitude;

    region.center.longitude = coordinate.longitude;

    region.span = MKCoordinateSpanMake(0.001, 0.001);

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

}

– (void)showHummingBird

{

    CLLocationCoordinate2D coordinate;

    coordinate.longitude = –75.1489669;

    coordinate.latitude = –14.6921048;

    [self moveAtCoordinate:coordinate];

    

    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”hummingBird”]];

    iv.transform = CGAffineTransformMakeScale(0.25, 0.25);

    iv.alpha = 0;

    iv.center = CGPointMake(180, 250);

    [self.view addSubview:iv];

    

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”opacity”];

    anim.fromValue = @0;

    anim.toValue = @0.6;

    anim.repeatCount = 6;

    anim.autoreverses = YES;

    anim.duration = 1.0;

    [iv.layer addAnimation:anim forKey:nil];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end