1 //重寫drawRect: 2 - (void)drawRect:(CGRect)rect { 3 // 1.得到圖形上下文 4 CGContextRef ctx = UIGraphicsGetCurrentContext(); 5 6 // 2.拼接圖形 7 // 2.1設置一個起點 8 CGContextMoveToPoint(ctx, 10, 10); 9 10 // 2.2添加一條線段,是從(10,10)到(100,100) 11 CGContextAddLineToPoint(ctx, 100, 100); 12 13 // 2.3從上次的位置開始再添加一條線段,是從(100,100)到(150,40) 14 CGContextAddLineToPoint(ctx, 150, 40); 15 16 // 2.4最後畫一條直線鏈接會原處,造成一個三角形 17 // CGContextAddLineToPoint(ctx, 10, 10); 18 CGContextClosePath(ctx); // 回到起點 19 20 // 3.渲染顯示到view上面 21 CGContextStrokePath(ctx); 22 }
1 - (void)drawRect:(CGRect)rect { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 2.畫四邊形 6 CGContextAddRect(ctx, CGRectMake(10, 10, 80, 100)); 7 8 // 繪製空心圖形 9 // CGContextStrokePath(ctx); 10 11 // 繪製實心圖形 12 CGContextFillPath(ctx); 13 }
1 - (void) drawRound { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 2.1畫圓 6 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100)); 7 8 // 2.2橢圓 9 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 150, 100)); 10 11 // 渲染 12 CGContextStrokePath(ctx); 13 }
1 - (void) drawArc { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 2.圓弧 6 // X軸正方向爲0角度,最後一個參數1表明逆時針方向 7 CGContextAddArc(ctx, 100, 100, 50, 0, -M_PI, 1); 8 9 // 渲染 10 CGContextStrokePath(ctx); 11 }
1 // 2.圓弧 2 // X軸正方向爲0角度,最後一個參數1表明逆時針方向 3 CGContextAddArc(ctx, 100, 100, 50, 0, -M_PI, 0);
1 - (void) drawText { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 2.畫上文字 6 /** 7 * 若是使用已通過期的方法 CGContextShowText,因爲CG畫板是以左下角爲零點,因此字會上下顛倒過來 8 */ 9 NSString *text = @"hello, 你好啊"; 10 // [text drawAtPoint:CGPointZero withAttributes:nil]; 11 12 CGRect r = CGRectMake(50, 50, 100, 100); 13 CGContextAddRect(ctx, r); 14 CGContextFillPath(ctx); 15 16 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 17 dict[NSForegroundColorAttributeName] = [UIColor redColor]; // 前景色,就是字體顏色 18 dict[NSFontAttributeName] = [UIFont systemFontOfSize:20]; // 字體 19 [text drawInRect:r withAttributes:dict]; 20 21 // 渲染 22 CGContextStrokePath(ctx); 23 }
1 - (void) drawImg { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 取得圖片 6 UIImage *img = [UIImage imageNamed:@"M4"]; 7 8 // 畫上圖片 9 // [img drawAtPoint:CGPointZero]; // 原圖大小,可能顯示不全 10 [img drawInRect:CGRectMake(0, 0, 100, 200)]; // 填充方式默認是拉伸 11 12 // 渲染 13 CGContextStrokePath(ctx); 14 }
1 - (void) drawImg { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 取得圖片 6 UIImage *img = [UIImage imageNamed:@"M4Mini"]; // 小圖 7 8 // 畫上圖片 9 [img drawAsPatternInRect:CGRectMake(0, 0, 200, 200)]; // 重複,能夠用來作花紋 10 11 // 渲染 12 CGContextStrokePath(ctx); 13 }
1 - (void) drawImg { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 取得圖片 6 UIImage *img = [UIImage imageNamed:@"M4"]; // 大圖 7 // UIImage *img = [UIImage imageNamed:@"M4Mini"]; // 小圖 8 9 // 畫上圖片 10 // [img drawAtPoint:CGPointZero]; // 原圖大小,可能顯示不全 11 [img drawInRect:CGRectMake(0, 0, 100, 200)]; // 填充方式默認是拉伸 12 // [img drawAsPatternInRect:CGRectMake(0, 0, 200, 200)]; // 重複,能夠用來作花紋 13 14 // 文字 15 NSString *text = @"這是一個美女"; 16 [text drawInRect:CGRectMake(0, 0, 100, 30) withAttributes:nil]; 17 18 // 渲染 19 CGContextStrokePath(ctx); 20 }
1 - (void) contextStackDemo { 2 // 1.得到上下文 3 CGContextRef ctx = UIGraphicsGetCurrentContext(); 4 5 // 2.存儲上下文 6 CGContextSaveGState(ctx); 7 8 // 3.設置上下文 9 CGContextSetLineCap(ctx, kCGLineCapRound); 10 CGContextSetLineWidth(ctx, 10); 11 [[UIColor redColor] set]; 12 13 // 4.畫第一條直線 14 CGContextMoveToPoint(ctx, 10, 10); 15 CGContextAddLineToPoint(ctx, 100, 100); 16 17 // 渲染 18 CGContextStrokePath(ctx); 19 20 // 5.恢復上下文 21 CGContextRestoreGState(ctx); 22 23 // 6.第二條直線 24 CGContextMoveToPoint(ctx, 100, 10); 25 CGContextAddLineToPoint(ctx, 10, 100); 26 27 // 渲染 28 CGContextStrokePath(ctx); 29 }
1 - (void) testCTM { 2 CGContextRef ctx = UIGraphicsGetCurrentContext(); 3 4 CGContextSaveGState(ctx); 5 6 CGContextRotateCTM(ctx, M_PI_4 * 0.3); // 旋轉 7 CGContextScaleCTM(ctx, 0.5, 0.5); // 縮放 8 CGContextTranslateCTM(ctx, 100, 0); // 移動 9 10 CGContextAddRect(ctx, CGRectMake(10, 10, 100, 100)); 11 CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 60, 60)); 12 13 CGContextMoveToPoint(ctx, 200, 100); 14 CGContextAddLineToPoint(ctx, 50, 200); 15 16 CGContextStrokePath(ctx); 17 18 }
1 - (void) testClip { 2 CGContextRef ctx = UIGraphicsGetCurrentContext(); 3 4 // 畫一個圓 5 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 150, 150)); 6 7 // 裁剪 8 CGContextClip(ctx); 9 10 // 加上圖片 11 UIImage *img = [UIImage imageNamed:@"a9ec8a13632762d0092abc3ca2ec08fa513dc619"]; 12 [img drawInRect:CGRectMake(0, 0, 150, 150)]; 13 14 CGContextStrokePath(ctx); 15 }
1 - (void)setRadius:(CGFloat)radius { 2 _radius = radius; 3 4 // 調用重繪/刷幀方法 5 [self setNeedsDisplay]; 6 } 7 8 // 初始化控件的時候, drawRect只會調用一次 9 - (void)drawRect:(CGRect)rect { 10 CGContextRef ctx = UIGraphicsGetCurrentContext(); 11 12 CGContextAddArc(ctx, 125, 125, self.radius, M_PI * 2, 0, 1); 13 CGContextFillPath(ctx); // 實心圓 14 }
1 @interface ViewController () 2 - (IBAction)onSlideChange:(UISlider *)sender; 3 @property (weak, nonatomic) IBOutlet MyView *circleView; 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 // Do any additional setup after loading the view, typically from a nib. 12 } 13 14 - (void)didReceiveMemoryWarning { 15 [super didReceiveMemoryWarning]; 16 // Dispose of any resources that can be recreated. 17 } 18 19 - (IBAction)onSlideChange:(UISlider *)sender { 20 self.circleView.radius = sender.value * 100; 21 } 22 @end
1 - (void)awakeFromNib { 2 // 添加定時器 3 // [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES]; 4 5 // 刷新更快的工具 6 CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)]; // 建立 7 8 // 添加到消息循環,啓動 9 [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 10 } 11 12 - (void)drawRect:(CGRect)rect { 13 self.snowY += 1; 14 if (self.snowY >= self.frame.size.height) { 15 self.snowY = -100; 16 } 17 UIImage *image = [UIImage imageNamed:@"M2Mini"]; 18 [image drawAtPoint:CGPointMake(100, self.snowY)]; 19 }