-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayingCardView.m
More file actions
244 lines (206 loc) · 8.31 KB
/
PlayingCardView.m
File metadata and controls
244 lines (206 loc) · 8.31 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// PlayingCardView.m
// SuperCard
//
// Created by Priyanjana Bengani on 17/12/2013.
// Copyright (c) 2013 anothercookiecrumbles. All rights reserved.
//
#import "PlayingCardView.h"
@interface PlayingCardView()
@property (nonatomic) CGFloat faceCardScaleFactor;
@end
@implementation PlayingCardView
#pragma mark - Properties
@synthesize faceCardScaleFactor = _faceCardScaleFactor;
#define DEFAULT_FACE_CARD_SCALE_FACTOR 0.90
- (CGFloat) faceCardScaleFactor {
if (!_faceCardScaleFactor) _faceCardScaleFactor = DEFAULT_FACE_CARD_SCALE_FACTOR;
return _faceCardScaleFactor;
}
- (void) setFaceCardScaleFactor:(CGFloat)faceCardScaleFactor {
_faceCardScaleFactor = faceCardScaleFactor;
[self setNeedsDisplay];
}
- (void) setSuit:(NSString *)suit {
_suit = suit;
[self setNeedsDisplay];
}
- (void) setRank:(NSUInteger)rank {
_rank = rank;
[self setNeedsDisplay];
}
- (void) setFaceUp:(BOOL)faceUp {
_faceUp = faceUp;
[self setNeedsDisplay];
}
- (void) pinch:(UIPinchGestureRecognizer*) gesture {
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
self.faceCardScaleFactor *= gesture.scale;
gesture.scale = 1.0;
}
}
#pragma mark - Drawing
#define CORNER_FONT_STANDARD_HEIGHT 180.0
#define CORNER_RADIUS 12.0
- (CGFloat) cornerScaleFactor { return self.bounds.size.height / CORNER_FONT_STANDARD_HEIGHT; }
- (CGFloat) cornerRadius { return CORNER_RADIUS * [self cornerScaleFactor]; }
- (CGFloat) cornerOffset { return [self cornerRadius] / 3.0; }
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
UIBezierPath* roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:[self cornerRadius]];
[roundedRect addClip];
[[UIColor whiteColor] setFill];
UIRectFill(self.bounds);
[[UIColor blackColor] setStroke];
[roundedRect stroke];
if (self.faceUp) {
NSString* imageName = [self determineImageForCard];
UIImage* faceImage = [UIImage imageNamed:imageName];
if (faceImage) {
CGRect imageRect = CGRectInset(self.bounds,
self.bounds.size.width * (1-self.faceCardScaleFactor),
self.bounds.size.height * (1-self.faceCardScaleFactor));
[faceImage drawInRect:imageRect];
}
else {
[self drawPips];
}
[self drawCorners];
}
else {
[[UIImage imageNamed:@"cardback"] drawInRect:self.bounds];
}
}
// This is horrible, but the image name with the characters from the SymbolCharacterSet was resulting in a nil image.
- (NSString*) determineImageForCard {
NSString* verboseSuit = [[NSString alloc] init];
if ([self.suit isEqualToString:@"♣︎"]) {
verboseSuit = [NSString stringWithFormat:@"Clubs"];
}
else if ([self.suit isEqualToString:@"♥︎"]) {
verboseSuit = [NSString stringWithFormat:@"Hearts"];
}
else if ([self.suit isEqualToString:@"♠︎"]) {
verboseSuit = [NSString stringWithFormat:@"Spades"];
}
else if ([self.suit isEqualToString:@"♦︎"]) {
verboseSuit = [NSString stringWithFormat:@"Diamonds"];
}
else {
NSLog(@"Unknown suit %@",self.suit);
}
return [NSString stringWithFormat:@"%@%@",[self rankAsString], verboseSuit];
}
- (NSString*) rankAsString {
return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"][self.rank];
}
#define PIP_FONT_SCALE_FACTOR 0.20
#define CORNER_OFFSET 2.0
- (void) drawCorners {
NSMutableParagraphStyle* paragraphStyle = [NSMutableParagraphStyle new]; // can use new instead of alloc-init
paragraphStyle.alignment = NSTextAlignmentCenter;
UIFont* cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
cornerFont = [cornerFont fontWithSize:cornerFont.pointSize * [self cornerScaleFactor]];
NSAttributedString* cornerText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@", [self rankAsString], self.suit] attributes:@{ NSFontAttributeName: cornerFont, NSParagraphStyleAttributeName: paragraphStyle }];
CGRect textBounds;
textBounds.origin = CGPointMake([self cornerOffset],[self cornerOffset]);
textBounds.size = [cornerText size];
[cornerText drawInRect:textBounds];
[self pushContextAndRotateUpsideDown];
[cornerText drawInRect:textBounds];
[self popContext];
}
- (void)pushContextAndRotateUpsideDown {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, self.bounds.size.width, self.bounds.size.height);
CGContextRotateCTM(context, M_PI);
}
- (void)popContext {
CGContextRestoreGState(UIGraphicsGetCurrentContext());
}
#pragma mark - Draw Pips
#define PIP_HOFFSET_PERCENTAGE 0.165
#define PIP_VOFFSET1_PERCENTAGE 0.090
#define PIP_VOFFSET2_PERCENTAGE 0.175
#define PIP_VOFFSET3_PERCENTAGE 0.270
- (void)drawPips
{
if ((self.rank == 1) || (self.rank == 5) || (self.rank == 9) || (self.rank == 3)) {
[self drawPipsWithHorizontalOffset:0
verticalOffset:0
mirroredVertically:NO];
}
if ((self.rank == 6) || (self.rank == 7) || (self.rank == 8)) {
[self drawPipsWithHorizontalOffset:PIP_HOFFSET_PERCENTAGE
verticalOffset:0
mirroredVertically:NO];
}
if ((self.rank == 2) || (self.rank == 3) || (self.rank == 7) || (self.rank == 8) || (self.rank == 10)) {
[self drawPipsWithHorizontalOffset:0
verticalOffset:PIP_VOFFSET2_PERCENTAGE
mirroredVertically:(self.rank != 7)];
}
if ((self.rank == 4) || (self.rank == 5) || (self.rank == 6) || (self.rank == 7) || (self.rank == 8) || (self.rank == 9) || (self.rank == 10)) {
[self drawPipsWithHorizontalOffset:PIP_HOFFSET_PERCENTAGE
verticalOffset:PIP_VOFFSET3_PERCENTAGE
mirroredVertically:YES];
}
if ((self.rank == 9) || (self.rank == 10)) {
[self drawPipsWithHorizontalOffset:PIP_HOFFSET_PERCENTAGE
verticalOffset:PIP_VOFFSET1_PERCENTAGE
mirroredVertically:YES];
}
}
- (void)drawPipsWithHorizontalOffset:(CGFloat)hoffset
verticalOffset:(CGFloat)voffset
upsideDown:(BOOL)upsideDown
{
if (upsideDown) [self pushContextAndRotateUpsideDown];
CGPoint middle = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
UIFont *pipFont = [UIFont systemFontOfSize:self.bounds.size.width * PIP_FONT_SCALE_FACTOR];
NSAttributedString *attributedSuit = [[NSAttributedString alloc] initWithString:self.suit attributes:@{ NSFontAttributeName : pipFont }];
CGSize pipSize = [attributedSuit size];
CGPoint pipOrigin = CGPointMake(
middle.x-pipSize.width/2.0-hoffset*self.bounds.size.width,
middle.y-pipSize.height/2.0-voffset*self.bounds.size.height
);
[attributedSuit drawAtPoint:pipOrigin];
if (hoffset) {
pipOrigin.x += hoffset*2.0*self.bounds.size.width;
[attributedSuit drawAtPoint:pipOrigin];
}
if (upsideDown) [self popContext];
}
- (void)drawPipsWithHorizontalOffset:(CGFloat)hoffset
verticalOffset:(CGFloat)voffset
mirroredVertically:(BOOL)mirroredVertically
{
[self drawPipsWithHorizontalOffset:hoffset
verticalOffset:voffset
upsideDown:NO];
if (mirroredVertically) {
[self drawPipsWithHorizontalOffset:hoffset
verticalOffset:voffset
upsideDown:YES];
}
}
#pragma mark - Initialisation
- (void) setup {
self.backgroundColor = nil; // could also do UIColor clearColor
self.opaque = NO;
self.contentMode = UIViewContentModeRedraw;
}
- (void) awakeFromNib {
[self setup]; // no alloc-init here
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end