我想連接3點,取決於最後一次點擊。例如,用戶點擊一次,點一個點,用戶再次點擊,它從抽頭1到抽頭2繪製一條線。最後,用戶再次點擊,並且從抽頭2到抽頭3形成一條線。如果用戶在敲擊時移動他的手指,它不會再畫,只是移動創建的點。我試圖從2點開始,但不會畫出階段。下面是我想:連接2點drawRect?
uiviewSubclass.h
@property (nonatomic) CGPoint firstPoint;
@property (nonatomic) CGPoint secondPoint;
@property (nonatomic) CGPoint thirdPoint;
uiviewSubclass.m
@implementation uiviewSubclass
{
UIBezierPath *path;
UIImage *incrementalImage;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self setMultipleTouchEnabled:NO];
[self setBackgroundColor:[UIColor clearColor]];
path = [UIBezierPath bezierPath];
[path setLineWidth:5.0];
self.firstPoint = CGPointZero;
self.secondPoint = CGPointZero;
self.thirdPoint = CGPointZero;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
UITouch *touch = [touches anyObject];
self.firstPoint = [touch locationInView:self];
[path moveToPoint:self.firstPoint];
self.hasSignature = @"YES";
}
else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
UITouch *touch = [touches anyObject];
self.secondPoint = [touch locationInView:self];
[path moveToPoint:self.secondPoint];
self.hasSignature = @"YES";
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
UITouch *touch = [touches anyObject];
self.firstPoint = [touch locationInView:self];
[path moveToPoint:self.firstPoint];
[self setNeedsDisplay];
}
else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
UITouch *touch = [touches anyObject];
self.secondPoint = [touch locationInView:self];
[path addLineToPoint:self.secondPoint];
[self setNeedsDisplay];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *){
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[incrementalImage drawInRect:rect]; // (3)
[[UIColor redColor] setStroke];
[path stroke];
}
爲什麼它不工作的任何想法?我是新來drawRect的東西,我爲任何初學者錯誤道歉。