2013-11-24 49 views
0

我想連接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的東西,我爲任何初學者錯誤道歉。

回答

1

我建議你使用UITapGestureRecognizer與你的看法。當每個敲擊被識別時,用每個點更新路徑。每次更新路徑時請致電setNeedsDisplay

您的drawRect:方法看起來正確。所以重要的是你的觸摸邏輯更新路徑。

1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    mouseSwiped = NO; 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) { 
     [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]]; 
     return; 
    } 

    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 20; 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.drawImage]; 
    currentPoint.y -= 20; 

    NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y); 

    UIGraphicsBeginImageContext(self.drawImage.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

}