Quartz2D
一、基本繪制:
- 畫直線
-(void)drawRect:(CGRect)rect {
//1.獲得一個(gè)view相關(guān)的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1.設(shè)置起點(diǎn)
[path moveToPoint:CGPointMake(10, 100)];
//2.2.設(shè)置終點(diǎn)
[path addLineToPoint:CGPointMake(200, 100)];
//3.把路徑添加到上下文
CGContextAddPath(context, path.CGPath);
//4.把上下文的內(nèi)容顯示到view上 fill stroke
CGContextStrokePath(context);
}
- 畫曲線
-(void)drawRect:(CGRect)rect {
//1.獲得一個(gè)view相關(guān)的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1.設(shè)置起點(diǎn)
[path moveToPoint:CGPointMake(10, 100)];
//2.2.設(shè)置終點(diǎn),和控制點(diǎn)
[path addQuadCurveToPoint:CGPointMake(230, 230) controlPoint:CGPointMake(100, 30)];
//3.把路徑添加到上下文
CGContextAddPath(context, path.CGPath);
//4.把上下文的內(nèi)容顯示到view上 fill stroke
CGContextStrokePath(context);
}
- 畫矩形
3.1 畫普通矩形:
-(void)drawRect:(CGRect)rect {
//1.獲得一個(gè)view相關(guān)的上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
//3.把路徑添加到上下文
CGContextAddPath(context, path.CGPath);
//4.把上下文的內(nèi)容顯示到view上 fill stroke
CGContextStrokePath(context);
}
3.2 畫圓角矩形:
-(void)drawRect:(CGRect)rect {
//1.描述路徑
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:50];
[[UIColor yellowColor] setFill];
//2.繪制圖形
[path fill];
}
3.3 指定某一個(gè)角為圓角:
-(void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(1000, 10)];
[path stroke];
}
- 畫圓或者橢圓
-(void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 200, 40)];
[path stroke];
}
- 畫圓弧
-(void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(120, 120) radius:40 startAngle:M_PI_4 endAngle:M_PI_4 * 3 clockwise:NO];
[[UIColor blueColor] set];
[path stroke];
}
- 畫扇形
6.1. 畫扇形1
-(void)drawRect:(CGRect)rect {
CGPoint point = CGPointMake(125, 125);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:point radius:100 startAngle:-M_PI_4 endAngle:-M_PI_4 * 3 clockwise:NO];
[path addLineToPoint:point];
[path closePath];
[path stroke];
}
6.2. 畫扇形2
-(void)drawRect:(CGRect)rect {
CGPoint point = CGPointMake(125, 125);
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:point radius:100 startAngle:-M_PI_4 endAngle:-M_PI_4*3 clockwise:NO];
[path addLineToPoint:point];
[path fill];
}
- 畫餅圖
-(void)drawRect:(CGRect)rect {
NSArray *angles = @[@30,@30,@40];
CGFloat endA = 0;
CGPoint point = CGPointMake(120, 120);
for (NSNumber *num in angles) {
CGFloat startA = endA;
endA = startA + num.intValue / 100.0 * 2 * M_PI;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:point radius:80 startAngle:startA endAngle:endA clockwise:YES];
[path addLineToPoint:point];
//自定義隨機(jī)顏色
[[self drawColor] set];
[path fill];
}
}
- 畫文字
-(void)drawRect:(CGRect)rect {
NSString *str = @"北京大學(xué)北京大學(xué)北京大學(xué)北京大學(xué)北京大學(xué)";
CGPoint point = CGPointMake(20, 0);
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:50];
attributes[NSForegroundColorAttributeName] = [UIColor greenColor];
//邊框?qū)挾? attributes[NSStrokeWidthAttributeName] = @4;
//必須要設(shè)置邊框?qū)挾?,否則顯示不出來陰影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blueColor];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowBlurRadius = 2;
attributes[NSShadowAttributeName] = shadow;
//不會(huì)自動(dòng)換行
[str drawAtPoint:point withAttributes:attributes];
//會(huì)自動(dòng)換行
// [str drawInRect:rect withAttributes:attributes];
}
- 繪制圖片
-(void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"head-1"];
//顯示原始大小
[image drawAtPoint:CGPointZero];
//充滿整個(gè)屏幕
// [image drawInRect:rect];
//平鋪整個(gè)屏幕
// [image drawAsPatternInRect:rect];
}
- 狀態(tài)棧
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 120)];
[path addLineToPoint:CGPointMake(230, 120)];
//保存狀態(tài)
CGContextSaveGState(context);
//將路徑添加到上下文
CGContextSetLineWidth(context, 5);
[[UIColor yellowColor] set];
CGContextAddPath(context, path.CGPath);
CGContextStrokePath(context);
//第二條直線
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 10)];
[path addLineToPoint:CGPointMake(100, 230)];
CGContextRestoreGState(context);
CGContextAddPath(context, path.CGPath);
CGContextStrokePath(context);
}
- 上下文的矩陣操作
-(void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
//平移操作,矩陣操作要在路徑添加到上下文之前
CGContextTranslateCTM(context, 100, 50);
//縮放
CGContextScaleCTM(context, 0.8, 0.8);
//旋轉(zhuǎn)
CGContextRotateCTM(context, M_PI_4);
CGContextAddPath(context, path.CGPath);
CGContextFillPath(context);
}
二、實(shí)現(xiàn)系統(tǒng)的UIImageView功能
#import <UIKit/UIKit.h>
@interface YJWImageView : UIView
/** 圖片 */
@property(nonatomic,strong) UIImage *img;
-(instancetype)initWithImage:(UIImage *)img;
@end
#import "YJWImageView.h"
@implementation YJWImageView
-(instancetype)initWithImage:(UIImage *)img
{
if (self == [super init]) {
self.frame = CGRectMake(0, 0, img.size.width, img.size.height);
_img = img;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self.img drawInRect:rect];
}
-(void)setImg:(UIImage *)img
{
_img = img;
[self setNeedsDisplay];
}
@end
三、實(shí)現(xiàn)定時(shí)器雪花功能
#import "YJWSnow.h"
@interface YJWSnow () //YJWSnow繼承自UIView,指定一個(gè)view的class為YJWSnow
/** snowY */
@property(nonatomic,assign) CGFloat snowY;
@end
@implementation YJWSnow
-(void)awakeFromNib
{
[super awakeFromNib];
//添加一個(gè)定時(shí)器,這個(gè)方法不好,因?yàn)槎〞r(shí)器設(shè)置的時(shí)間和屏幕刷新的時(shí)間不一定相同,可能會(huì)出現(xiàn)卡頓的現(xiàn)象
// [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(update) userInfo:nil repeats:YES];
//創(chuàng)建CADisplayLink,每次當(dāng)屏幕刷新時(shí)候調(diào)用,屏幕每秒刷新60次
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)update
{
_snowY += 1;
[self setNeedsDisplay];
if (_snowY >= [UIScreen mainScreen].bounds.size.height) {
_snowY = 0;
}
}
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"雪花"];
[img drawAtPoint:CGPointMake(0, _snowY)];
}
@end
四、給圖片添加水印
-(void)shuiyinImg
{
//1.獲取圖片
UIImage *image = [UIImage imageNamed:@"阿貍頭像"];
//2.開啟圖片上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 1);
//3.把圖片繪制給上下文
[image drawAtPoint:CGPointZero];
//4.繪制文字
NSString *str = @"Yijiang";
[str drawAtPoint:CGPointZero withAttributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:28],
NSForegroundColorAttributeName:[UIColor greenColor]
}];
//5.生成圖片
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//6.關(guān)閉圖片上下文
UIGraphicsEndImageContext();
self.imgV.image = img;
}
五、生成圓形圖片
-(void)clipImg
{
//1.獲取圖片
UIImage *image = [UIImage imageNamed:@"阿貍頭像"];
//2.開啟圖片上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
//3.設(shè)置裁剪路徑
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[path addClip];
//4.把圖片繪制到上下文
[image drawAtPoint:CGPointZero];
//5.獲取圖片
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//6.關(guān)閉圖片上下文
UIGraphicsEndImageContext();
self.imgV.image = img;
}
六、裁剪出帶有邊框的圖片
-(void)borderClip
{
//1.獲取圖片
UIImage *image = [UIImage imageNamed:@"阿貍頭像"];
//2.設(shè)置邊框大小
CGFloat border = 10;
//3.開啟圖片上下文
//size 設(shè)置為包含邊框的大小
CGSize size = CGSizeMake(image.size.width + 2 * border, image.size.height + 2 * border);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
//4.描述一個(gè)大圓,設(shè)為填充
UIBezierPath *bezier1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
//設(shè)置填充顏色
[[UIColor greenColor] set];
[bezier1 fill];
//5.描述一個(gè)小圓,設(shè)為裁剪路徑
UIBezierPath *bezier2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, image.size.width, image.size.height)];
[bezier2 addClip];
//6.繪制圖片
[image drawInRect:CGRectMake(border, border, image.size.width, image.size.height)];
//7.獲取新圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageV.image = newImage;
//8.關(guān)閉上下文
UIGraphicsEndImageContext();
}
七、截屏
-(void)shot
{
//1.開啟圖片上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
//2.獲取當(dāng)前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//3.UIView之所以能夠顯示,是因?yàn)樗鼉?nèi)部有一個(gè)層,layer層是通過渲染的方式繪制上下文。
[self.view.layer renderInContext:context];
//4.獲取所截圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//5.關(guān)閉圖片上下文
UIGraphicsEndImageContext();
self.imageV.image = image;
}