借鉴于斯坦福课程
.h
<span style="font-size:18px;">#import <UIKit/UIKit.h>
@interface DrawingView : UIView
@property (nonatomic) NSInteger rank;
@property (nonatomic, strong) NSString *suit;
@property (nonatomic) BOOL faceUp;
- (void)pinch:(UIPinchGestureRecognizer *)gesture;
@end</span>
.m
<span style="font-size:18px;">#import "DrawingView.h"
@interface DrawingView ()
@property (nonatomic) CGFloat faceCardScaleFactor;
@end
@implementation DrawingView
@synthesize faceCardScaleFactor = _faceCardScaleFactor;//因为同时自定义了setter和getter方法。
#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:(NSInteger)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;
}
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (NSString *)rankAsString{
return @[@"?", @"A", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K", ][self.rank];
}
#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; }
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *roundRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:12.0];
[roundRect addClip];
[[UIColor whiteColor] setFill];
UIRectFill(self.bounds);
[[UIColor blackColor]setStroke];
[roundRect stroke];
if (self.faceUp) {
UIImage *faceImage = [UIImage imageNamed:@"2"];
if (faceImage) {
CGRect imageRect = CGRectInset(self.bounds, self.bounds.size.width * (1.0-self.faceCardScaleFactor), self.bounds.size.height * (1.0-self.faceCardScaleFactor));
[faceImage drawInRect:imageRect];
}else {
[self drawPips];
}
[self drawCorners];
}else{
[[UIImage imageNamed:@"1"]drawInRect:self.bounds];
}
}
- (void)drawPips{
}
- (void)drawCorners{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle 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];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.bounds.size.width, self.bounds.size.height);
CGContextRotateCTM(context, M_PI);
[cornerText drawInRect:textBounds];
}
- (void)setup{
self.backgroundColor = nil;
self.opaque = NO;
self.contentMode = UIViewContentModeRedraw;
}
- (void)awakeFromNib{
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
@end
</span>
使用举例:
<span style="font-size:18px;">@property (weak, nonatomic) IBOutlet DrawingView *myDrawingView;</span>
<span style="font-size:18px;">self.myDrawingView.suit = @"♥️";
self.myDrawingView.rank = 13;
[self.myDrawingView addGestureRecognizer:[[UIPinchGestureRecognizer alloc]initWithTarget:self.myDrawingView action:@selector(pinch:)]];</span>
<span style="font-size:18px;">- (IBAction)swip:(UISwipeGestureRecognizer *)sender {
self.myDrawingView.faceUp = !self.myDrawingView.faceUp;
}</span>