涂鸦-每次调setNeedsDisplay以后就会重新调用一次drawRect方法,每次调drawRect方法就会把之前画好的东西删掉
2014-09-11 19:41
330 查看
//
// WJView.m
// zwj涂鸦
//
// Created by zwj on 14-9-9.
// Copyright (c) 2014年 zwj. All rights reserved.
//
#import "WJView.h"
@interface WJView()
@property(nonatomic,strong) NSMutableArray *allPaths;
@end
@implementation WJView
- (void)backto{
[self.allPaths removeLastObject];
[self setNeedsDisplay];
}
- (void)clearScreen{
[self.allPaths removeAllObjects];
[self setNeedsDisplay];
}
-(NSMutableArray *)allPaths{
if (_allPaths == nil) {
_allPaths = [NSMutableArray array];
}
return _allPaths;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:touch.view];
NSMutableArray *aryCurrentP = [NSMutableArray array];
[aryCurrentP addObject:[NSValue valueWithCGPoint:currentPoint]];
[self.allPaths addObject:aryCurrentP];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:touch.view];
NSMutableArray *ary = [self.allPaths lastObject];
[ary addObject:[NSValue valueWithCGPoint:currentP]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:touch.view];
NSMutableArray *ary = [self.allPaths lastObject];
[ary addObject:[NSValue valueWithCGPoint:currentP]];
[self setNeedsDisplay];
}
/**
* 绘制图形
*/
-(void)drawRect:(CGRect)rect{
CGContextRef ref = UIGraphicsGetCurrentContext();
for (NSMutableArray *ary in self.allPaths) {
for (int i = 0; i < ary.count; i++) {
if (i == 0) {
CGPoint firstPoint = [ary[i] CGPointValue];
CGContextMoveToPoint(ref, firstPoint.x, firstPoint.y);
} else {
CGPoint movePoint = [ary[i] CGPointValue];
CGContextAddLineToPoint(ref, movePoint.x, movePoint.y);
}
}
}
CGContextSetLineJoin(ref, kCGLineJoinRound);
CGContextSetLineWidth(ref, 5);
CGContextStrokePath(ref);
}
@end
// WJView.m
// zwj涂鸦
//
// Created by zwj on 14-9-9.
// Copyright (c) 2014年 zwj. All rights reserved.
//
#import "WJView.h"
@interface WJView()
@property(nonatomic,strong) NSMutableArray *allPaths;
@end
@implementation WJView
- (void)backto{
[self.allPaths removeLastObject];
[self setNeedsDisplay];
}
- (void)clearScreen{
[self.allPaths removeAllObjects];
[self setNeedsDisplay];
}
-(NSMutableArray *)allPaths{
if (_allPaths == nil) {
_allPaths = [NSMutableArray array];
}
return _allPaths;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:touch.view];
NSMutableArray *aryCurrentP = [NSMutableArray array];
[aryCurrentP addObject:[NSValue valueWithCGPoint:currentPoint]];
[self.allPaths addObject:aryCurrentP];
[self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:touch.view];
NSMutableArray *ary = [self.allPaths lastObject];
[ary addObject:[NSValue valueWithCGPoint:currentP]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentP = [touch locationInView:touch.view];
NSMutableArray *ary = [self.allPaths lastObject];
[ary addObject:[NSValue valueWithCGPoint:currentP]];
[self setNeedsDisplay];
}
/**
* 绘制图形
*/
-(void)drawRect:(CGRect)rect{
CGContextRef ref = UIGraphicsGetCurrentContext();
for (NSMutableArray *ary in self.allPaths) {
for (int i = 0; i < ary.count; i++) {
if (i == 0) {
CGPoint firstPoint = [ary[i] CGPointValue];
CGContextMoveToPoint(ref, firstPoint.x, firstPoint.y);
} else {
CGPoint movePoint = [ary[i] CGPointValue];
CGContextAddLineToPoint(ref, movePoint.x, movePoint.y);
}
}
}
CGContextSetLineJoin(ref, kCGLineJoinRound);
CGContextSetLineWidth(ref, 5);
CGContextStrokePath(ref);
}
@end
相关文章推荐
- 关于setNeedsDisplay、setNeedsLayout方法,UIView的layoutSubviews和drawRect方法何时调用
- IOS_Quartz2D_水印缩略图_PDF_setNeedsDisplay方法内部自动调用drawRect
- [iOS] 在循环中使用setNeedsDisplay来调用drawRect的方法
- iOS UIView的drawRect方法 - setNeedsDisplay和setNeedsDisplayInRect.
- setNeedsDisplay和setNeedsLayout 区别 layoutSubviews 和drawRect被调用时机
- iOS setNeedsDisplay不能正常调起drawRect方法的某个原因
- setNeedsDisplay,setNeedsLayout,layoutSubviews,drawRect的调用关系
- layoutSubviews、setNeedsLayout、setNeedsDisplay、drawRect区别及调用的时机
- 重新调用drawRect方法
- UIView的setNeedsDisplay和setNeedsLayout方法
- UIView的setNeedsDisplay和setNeedsLayout方法
- UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews
- 关于setNeedsDisplay、setNeedsLayout方法
- UIView的setNeedsDisplay和setNeedsLayout方法
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout 还有drawRect方法
- UIView的setNeedsDisplay和setNeedsLayout方法
- signal signal函数每次设置具体的信号处理函数(非SIG_IGN)只能生效一次,多次调用需要调用时在加类似监听的方法!!! 最好用sigaction
- [self setNeedsDisplay]&&drawRect几个小点
- layoutSubviews何时调用的问题 ,setNeedsDisplay和setNeedsLayout
- UIView的setNeedsDisplay和setNeedsLayout方法