Created
September 21, 2015 14:29
-
-
Save mzsima/fc97d4aad3957a497890 to your computer and use it in GitHub Desktop.
clock number
This file contains hidden or 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
// | |
// ViewController.m | |
// clockNumber | |
// | |
// Created by MizushimaYusuke on 9/21/15. | |
// Copyright © 2015 MizushimaYusuke. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ShapeLayerHitCustom : CAShapeLayer | |
@end | |
@implementation ShapeLayerHitCustom | |
- (BOOL)containsPoint:(CGPoint)p | |
{ | |
return CGPathContainsPoint(self.path, NULL, p, false); | |
} | |
@end | |
@interface ViewController () | |
@property (nonatomic, weak) CALayer *selected; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.view.backgroundColor = [UIColor orangeColor]; | |
[self createClock]; | |
} | |
- (void)createClock { | |
CGPoint o = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); | |
float s = CGRectGetMaxX(self.view.bounds) * 0.84; | |
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, s, s)]; | |
v.center = o; | |
v.backgroundColor = [UIColor whiteColor]; | |
v.layer.cornerRadius = s * 0.5; | |
[self.view addSubview:v]; | |
UIBezierPath *path = [UIBezierPath bezierPath]; | |
[path moveToPoint:o]; | |
[path addLineToPoint:CGPointMake(o.x + s * 0.5, o.y)]; | |
[path addArcWithCenter:o radius:s * 0.5 startAngle:0 endAngle:M_PI / 6.0 clockwise:YES]; | |
[path closePath]; | |
float dw = M_PI / 6.0; | |
NSMutableArray *numbers = [NSMutableArray array]; | |
for (int i=1; i<=12; i++) [numbers addObject:@(i)]; | |
for (int i=1; i<=12; i++) { | |
float w = i * dw - dw * 3.5; | |
int n = [[numbers objectAtIndex:(arc4random() % (int)numbers.count)] intValue]; | |
[numbers removeObject:@(n)]; | |
CAShapeLayer *l = [ShapeLayerHitCustom layer]; | |
l.name = [NSString stringWithFormat:@"%d", n]; | |
l.frame = self.view.bounds; | |
l.path = path.CGPath; | |
l.fillColor = [UIColor colorWithHue:0.3 saturation:0.8 brightness:0.7 alpha:1].CGColor; | |
l.transform = CATransform3DMakeRotation(w, 0, 0, 1); | |
l.masksToBounds = YES; | |
[self.view.layer addSublayer:l]; | |
CATextLayer *t = [CATextLayer layer]; | |
t.bounds = CGRectMake(0, 0, 40, 40); | |
t.fontSize = 30; | |
t.backgroundColor = [UIColor clearColor].CGColor; | |
t.alignmentMode = kCAAlignmentCenter; | |
t.string = [NSString stringWithFormat:@"%d", n]; | |
t.position = CGPointMake(o.x + s * 0.4, o.y + 30); | |
t.transform = CATransform3DMakeRotation(-w, 0, 0, 1); | |
[l addSublayer:t]; | |
} | |
} | |
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { | |
CGPoint p = [[touches anyObject] locationInView:self.view]; | |
CALayer *l = [self.view.layer hitTest:p]; | |
if ([l isKindOfClass:[CATextLayer class]]) { | |
l = l.superlayer; | |
} | |
if (l.name) { | |
if (!self.selected) { | |
self.selected = l; | |
((CAShapeLayer*)l).fillColor = [UIColor colorWithHue:0.7 saturation:0.8 brightness:0.7 alpha:1].CGColor; | |
} else { | |
CATextLayer *a = (CATextLayer *)self.selected.sublayers[0]; | |
CATextLayer *b = (CATextLayer *)l.sublayers[0]; | |
NSString *aText = a.string; | |
a.string = b.string; | |
b.string = aText; | |
((CAShapeLayer*)self.selected).fillColor = [UIColor colorWithHue:0.3 saturation:0.8 brightness:0.7 alpha:1].CGColor; | |
((CAShapeLayer*)l).fillColor = [UIColor colorWithHue:0.7 saturation:0.8 brightness:0.7 alpha:1].CGColor; | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | |
((CAShapeLayer*)l).fillColor = [UIColor colorWithHue:0.3 saturation:0.8 brightness:0.7 alpha:1].CGColor; | |
}); | |
self.selected = nil; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4376