-
Notifications
You must be signed in to change notification settings - Fork 1
/
Star.m
85 lines (64 loc) · 2.09 KB
/
Star.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Star.m
// test
//
// Created by Satbir Tanda on 12/4/15.
// Copyright © 2015 Satbir Tanda. All rights reserved.
//
#import "Star.h"
@implementation Star
- (UIColor *)starColor
{
if (!_starColor) {
_starColor = [UIColor greenColor];
}
return _starColor;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath* starPath = [UIBezierPath bezierPath];
[self.starColor setFill];
CGFloat starExtrusion = CGRectGetMaxX(rect)/5.0;
CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGFloat pointsOnStar = 5;
CGFloat angle = -M_PI_2;
CGFloat angleIncrement = (2.0*M_PI / pointsOnStar);
CGFloat radius = rect.size.width/2.0;
BOOL firstPoint = YES;
for (int i = 1; i <= pointsOnStar; i++)
{
CGPoint point = [self pointFrom:angle
withRadius:radius
andOffset:center];
CGPoint nextPoint = [self pointFrom:angle + angleIncrement
withRadius:radius
andOffset:center];
CGPoint midPoint = [self pointFrom:angle + angleIncrement/2.0
withRadius:starExtrusion
andOffset:center];
if (firstPoint)
{
firstPoint = NO;
[starPath moveToPoint:point];
}
[starPath addLineToPoint:midPoint];
[starPath addLineToPoint:nextPoint];
angle += angleIncrement;
}
[starPath closePath];
[starPath fill];
}
- (CGPoint)pointFrom:(CGFloat)angle withRadius:(CGFloat)radius andOffset:(CGPoint)offset
{
return CGPointMake(radius *cos(angle) + offset.x, radius * sin(angle) + offset.y);
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
@end