先看图
![这里写图片描述](https://img-blog.csdn.net/20150924215207690)
![这里写图片描述](https://img-blog.csdn.net/20150924215228494)
![这里写图片描述](https://img-blog.csdn.net/20150924215247776)
![这里写图片描述](https://img-blog.csdn.net/20150924215310201)
代码下载请猛戳这里
使用路径绘制图形
/**
* 路径的使用
*
* @param x1 起点x
* @param y1 起点y
* @param x2 终点x
* @param y2 终点y
*/
void usedPathDraw(CGFloat selfWidth)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 10, 50);
CGPathAddLineToPoint(path, NULL, 90, 50);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGMutablePathRef path1 = CGPathCreateMutable();
CGPathMoveToPoint(path1, NULL, selfWidth/2 - 40, 50);
CGPathAddLineToPoint(path1, NULL, selfWidth/2 + 40, 50);
CGFloat lengths[] = {10,5};
CGContextSetLineDash(context, 0, lengths, 2);
CGContextAddPath(context, path1);
CGContextStrokePath(context);
CGMutablePathRef path2 = CGPathCreateMutable();
CGPathMoveToPoint(path2, NULL, selfWidth - 90, 50);
CGPathAddLineToPoint(path2, NULL, selfWidth - 10, 50);
CGFloat lengths1[] = {7,3,5,4,2,6,1};
CGContextSetLineDash(context, 0, lengths1, 7);
CGContextSetLineWidth(context, 5);
CGContextAddPath(context, path2);
CGContextStrokePath(context);
CGPathRelease(path);
CGContextRestoreGState(context);
}
贝瑟尔路径绘制图形
/**
* 贝瑟尔路径的使用
*/
- (void)usedBezierPath
{
CGPoint center = CGPointMake(50, 150);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:40 startAngle:0 endAngle:M_PI*1.5 clockwise:YES];
[path addLineToPoint:center];
[path closePath];
[path stroke];
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(Self_Width/2 - 40, 110, 80, 80) cornerRadius:10];
[path stroke];
}
合成图片
/**
* 合成图片
*
* @param basicImage 背景图
* @param otherImage 前景图
* @param size 合成的图像大小
*
* @return 合成后的图像
*/
- (UIImage *)imageSynthesis:(UIImage *)basicImage andOther:(UIImage *)otherImage andSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[basicImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
CGFloat spacingProportion = 0.08;
CGFloat fProportion = 0.1;
CGFloat fWidth = size.width*fProportion;
CGFloat fHeight = size.height*fProportion;
[otherImage drawInRect:CGRectMake(size.width*(1 - spacingProportion) - fWidth, size.height*(1 - spacingProportion) - fHeight, fWidth, fHeight)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
裁剪图像
- (UIImage *)cutImage:(UIImage *)image
{
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, image.size.width, image.size.height) cornerRadius:10];
[path addClip];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
UIImage *endImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return endImage;
}
截屏
/**
* 截屏
*
* @param view 需要截屏的view
*
* @return 截屏后生成的图片
*/
- (UIImage *)screenshots:(UIView *)view
{
if (view == nil) return nil;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
平铺图像
/**
* 平铺图像
*/
- (void)backgroundTile
{
CGFloat rowW = Self_Width/3 + 0.1;
CGFloat rowH = 100;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddRect(context, CGRectMake(0, 0, Self_Width, rowH));
[[UIColor whiteColor] setFill];
CGContextFillPath(context);
CGFloat lineH = 1;
CGContextMoveToPoint(context, 0, rowH - lineH);
CGContextAddLineToPoint(context, rowW - lineH, rowH - lineH);
CGContextAddLineToPoint(context, rowW - lineH, 0);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
self.backgroundColor = [UIColor colorWithPatternImage:image];
}
绘制扑克牌
#import "PlayingCardView.h"
@interface PlayingCardView()
{
NSInteger count;
}
@property (nonatomic,assign)CGFloat faceCardScaleFactor;
@end
@implementation PlayingCardView
@synthesize faceCardScaleFactor = _faceCardScaleFactor;
#pragma mark - Properties
- (void)setRank:(NSInteger)rank
{
if (_rank != rank) {
_rank = rank;
[self setNeedsDisplay];
}
}
- (void)setSuit:(NSString *)suit
{
if (_suit != suit) {
_suit = suit;
[self setNeedsDisplay];
}
}
- (void)setFaceUp:(BOOL)faceUp
{
if (_faceUp != faceUp) {
_faceUp = faceUp;
[self setNeedsDisplay];
}
}
- (void)setFaceCardScaleFactor:(CGFloat)faceCardScaleFactor
{
if (_faceCardScaleFactor != faceCardScaleFactor) {
_faceCardScaleFactor = faceCardScaleFactor;
[self setNeedsDisplay];
}
}
#define DEFAULR_FACE_CARD_SCALE_FACTOR 0.90
- (CGFloat)faceCardScaleFactor
{
if (!_faceCardScaleFactor) {
_faceCardScaleFactor = DEFAULR_FACE_CARD_SCALE_FACTOR;
}
return _faceCardScaleFactor;
}
- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
self.faceCardScaleFactor *= gesture.scale;
gesture.scale = 1;
}
}
#pragma mark - Drawing
#define CORNER_FCNT_STANDRAD_HEIGHT 180.0
#define CORNER_RADIUS 12.0
- (float)cornerScaleFactor {return self.bounds.size.height/CORNER_FCNT_STANDRAD_HEIGHT;}
- (float)cornerRadius {return CORNER_RADIUS*[self cornerScaleFactor];}
- (float)cornerOffset {return [self cornerRadius]/3.0;}
- (void)drawRect:(CGRect)rect {
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:[self cornerRadius]];
[roundedRect addClip];
[[UIColor whiteColor] setFill];
[roundedRect fill];
if (self.faceUp) {
if (!count) {
count = 1;
}
NSLog(@"%i",count);
count++;
UIImage *faceImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",self.suit,[self rankStrings]]];
if (faceImage)
{
CGRect imageRect = CGRectInset(self.bounds,
self.bounds.size.height*(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];
}
[[UIColor blackColor] setStroke];
[roundedRect setLineWidth:5];
[roundedRect stroke];
}
- (NSString *)rankStrings
{
return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"][self.rank];
}
- (void)drawCorners
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
UIFont *cornerFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
cornerFont = [cornerFont fontWithSize:[cornerFont pointSize]*[self cornerScaleFactor]];
UIColor *cornerColor;
BOOL isRed = [self.suit isEqualToString:@"♥️"];
BOOL isRed1 = [self.suit isEqualToString:@"♦️"];
if (isRed||isRed1)
{
cornerColor = [UIColor redColor];
}
else
{
cornerColor = [UIColor blackColor];
}
NSAttributedString *cornerText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",[self rankStrings],self.suit]
attributes:@{NSFontAttributeName:cornerFont,
NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:cornerColor}];
CGRect textBounds;
textBounds.origin = CGPointMake([self cornerOffset], 2*[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];
}
#pragma mark - Pips
#define PIP_HOFFSET_PERCENTAGE 0.200
#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 == 3 || self.rank == 5 || self.rank == 9) {
[self drawPipsWithHerizontalOffset:0
vertiCalOffset:0
mirroredVertically:NO];
}
if (self.rank == 6 || self.rank == 7 || self.rank == 8) {
[self drawPipsWithHerizontalOffset:PIP_HOFFSET_PERCENTAGE
vertiCalOffset:0
mirroredVertically:NO];
}
if (self.rank == 2 || self.rank == 3) {
[self drawPipsWithHerizontalOffset:0
vertiCalOffset:PIP_VOFFSET3_PERCENTAGE
mirroredVertically:YES];
}
if (self.rank == 7 || self.rank == 8 || self.rank == 10) {
[self drawPipsWithHerizontalOffset: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 drawPipsWithHerizontalOffset:PIP_HOFFSET_PERCENTAGE
vertiCalOffset:PIP_VOFFSET3_PERCENTAGE
mirroredVertically:YES];
}
if (self.rank == 9 || self.rank == 10) {
[self drawPipsWithHerizontalOffset:PIP_HOFFSET_PERCENTAGE
vertiCalOffset:PIP_VOFFSET1_PERCENTAGE
mirroredVertically:YES];
}
}
#define PIP_FONT_SCALE_FACTOR 0.012
- (void)drawPipsWithHerizontalOffset:(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 preferredFontForTextStyle:UIFontTextStyleBody];
pipFont = [pipFont fontWithSize:[pipFont pointSize]*self.bounds.size.width*PIP_FONT_SCALE_FACTOR];
NSAttributedString *attributedSuit = [[NSAttributedString alloc] initWithString:self.suit
attributes:@{NSFontAttributeName:pipFont}];
CGSize pipSize = [attributedSuit size];
CGPoint pipDrigin = CGPointMake(middle.x-pipSize.width/2-hoffSet*self.bounds.size.width, middle.y-pipSize.height/2-voffSet*self.bounds.size.height);
[attributedSuit drawAtPoint:pipDrigin];
if (hoffSet) {
pipDrigin.x += hoffSet*2*self.bounds.size.width;
[attributedSuit drawAtPoint:pipDrigin];
}
if (upsideDown) {
[self popContext];
}
}
- (void)drawPipsWithHerizontalOffset:(CGFloat)hoffSet
vertiCalOffset:(CGFloat)voffSet
mirroredVertically:(BOOL)mirroredVertically
{
[self drawPipsWithHerizontalOffset:hoffSet
vertiCalOffset:voffSet
upsideDown:NO];
if (mirroredVertically) {
[self drawPipsWithHerizontalOffset:hoffSet
vertiCalOffset:voffSet
upsideDown:YES];
}
}
- (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 - Initialization
- (void)setUp
{
self.backgroundColor = nil;
self.opaque = NO;
self.contentMode = UIViewContentModeRedraw;
}
- (void)awakeFromNib
{
[self setUp];
}
@end