真ん中に表示されたのと、おんなじ角度の扇形を探してタッチするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic) float angle;
@property (nonatomic) float currentAngle;
@property (nonatomic, weak) CAShapeLayer *blue;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[self createCenter];
[self createAround];
UILabel *title = [[UILabel alloc] init];
title.text = @”angle”;
title.font = [UIFont fontWithName:@”Chalkduster” size:30];
title.textColor = [UIColor darkGrayColor];
[title sizeToFit];
title.center = CGPointMake(CGRectGetMidX(self.view.frame), 45);
[self.view addSubview:title];
}
– (void)createCenter
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:110 startAngle:0 endAngle:2.0 * M_PI clockwise:YES];
CAShapeLayer *l = [CAShapeLayer layer];
l.path = path.CGPath;
l.lineWidth = 5;
l.strokeColor = [UIColor redColor].CGColor;
l.fillColor = [UIColor whiteColor].CGColor;
l.position = CGPointMake(160, 280);
[self.view.layer addSublayer:l];
UIBezierPath *bPath = [UIBezierPath bezierPath];
[bPath moveToPoint:CGPointZero];
[bPath addLineToPoint:CGPointMake(0, –110)];
self.blue = [CAShapeLayer layer];
self.blue.path = bPath.CGPath;
self.blue.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.1].CGColor;
self.blue.strokeColor = [UIColor blueColor].CGColor;
self.blue.lineWidth = 4;
self.blue.position = l.position;
self.blue.lineJoin = kCALineJoinRound;
[self.view.layer addSublayer:self.blue];
UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
start.frame = CGRectMake(0, 0, 40, 40);
start.backgroundColor = [[UIColor brownColor] colorWithAlphaComponent:0.4];
start.center = l.position;
start.transform = CGAffineTransformMakeRotation(M_PI/4.0);
[self.view addSubview:start];
[start addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
}
– (void)start
{
float dAngle = 2.0 * M_PI / 32.0;
self.currentAngle = 0;
self.angle = dAngle * (arc4random() % 32);
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate:) userInfo:nil repeats:YES];
}
– (void)rotate:(NSTimer *)sender
{
self.currentAngle += M_PI / 32.0;
if (self.currentAngle < self.angle) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(0, –110)];
[path addArcWithCenter:CGPointZero radius:110 startAngle:-M_PI/2.0 endAngle:self.currentAngle – M_PI/2.0 clockwise:YES];
[path closePath];
self.blue.path = path.CGPath;
} else {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(0, –110)];
[path addArcWithCenter:CGPointZero radius:110 startAngle:-M_PI/2.0 endAngle:self.angle – M_PI/2.0 clockwise:YES];
[path closePath];
self.blue.path = path.CGPath;
[sender invalidate];
sender = nil;
}
}
– (void)createAround
{
for (int i=0; i<32; i++) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.tag = i;
b.frame = CGRectMake(0, 0, 38, 38);
b.backgroundColor = [UIColor brownColor];
[self.view addSubview:b];
[b addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
float x = 0, y = 0;
switch (i/8) {
case 0:
x = (i % 8) * 40 + 20;
y = 100;
break;
case 1:
x = 300;
y = (i % 8) * 40 + 140;
break;
case 2:
x = (7 – (i % 8)) * 40 + 20;
y = 460;
break;
case 3:
x = 20;
y = (7 – (i % 8)) * 40 + 140;
break;
default:
break;
}
b.center = CGPointMake(x, y);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(0, 14)];
[path addArcWithCenter:CGPointZero radius:14 startAngle:-M_PI/2.0 endAngle:i * (M_PI/16.0) – M_PI/2.0 clockwise:YES];
[path closePath];
if (i == 0) {
path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:14 startAngle:0 endAngle:2.0*M_PI clockwise:YES];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = path.CGPath;
l.fillColor = [UIColor whiteColor].CGColor;
l.lineWidth = 0;
l.position = CGPointMake(19, 19);
[b.layer addSublayer:l];
}
}
– (void)check:(UIButton *)sender
{
if (fabs(sender.tag * (M_PI/16.0) – self.angle) < 0.01) {
[UIView animateWithDuration:0.2 animations:^{
sender.transform = CGAffineTransformMakeRotation(M_PI/4.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
sender.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self start];
}];
}];
} else {
[UIView animateWithDuration:0.2 animations:^{
sender.transform = CGAffineTransformMakeScale(0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
sender.transform = CGAffineTransformIdentity;
}];
}];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end