iPhoneマップと回転View

マップの適当な座標に、アニメーションするViewを表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate> {

    CLLocationCoordinate2D markCd;

    CLLocationCoordinate2D lastCd;

}

@property (nonatomic, weak) MKMapView *mapView;

@property (nonatomic, weak) UIView *mark;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createMap];

}

– (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        [self createAnimationMark];

        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateMark) userInfo:nil repeats:YES];

    });

}

– (void)createMap

{

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

    mv.delegate = self;

    [self.view addSubview:mv];

    self.mapView = mv;

}

– (void)createAnimationMark

{

    UILabel *v = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    v.text = @”map\n+\nview\nrotation”;

    v.numberOfLines = 0;

    v.textAlignment = NSTextAlignmentCenter;

    v.textColor = [UIColor whiteColor];

    v.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

    v.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];

    

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];

    anim.toValue = @(2.0*M_PI);

    anim.repeatCount = HUGE_VAL;

    anim.duration = 3.0;

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

    

    [self.view addSubview:v];

    

    self.mark = v;

    markCd = [self.mapView convertPoint:self.mark.center toCoordinateFromView:self.view];

}

– (void)updateMark

{

    self.mark.center = [self.mapView convertCoordinate:markCd toPointToView:self.view];

}

@end