0

我只想在 ViewController 类型的同一个视图中的 2 个不同点之间画线。我已经编写了以下代码,但它没有显示线。请帮我解决这个问题。

- (void)drawRect:(CGRect)rect
{
[brushPattern setStroke];
[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}

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

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
//  CGPoint *point=mytouch 
[myPath moveToPoint:[mytouch locationInView:self.view]];
CGPoint pos = [mytouch locationInView: self.view];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);   
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self.view]];
[self.view setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
CGPoint pos = [mytouch locationInView: self.view];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);
}
4

1 回答 1

0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch* touch = [touches anyObject];

  currentPoint = [touch locationInView:self.image];
  UIGraphicsBeginImageContext(self.view.frame.size);
  if (!(last.x == 0 && last.y == 0)) {

    CGContextRef context    = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, 2.0);
    CGContextMoveToPoint(context, currentPoint.x,currentPoint.y);
     CGContextAddLineToPoint(context, last.x, last.y);
    CGContextStrokePath(context);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
    [image setNeedsDisplay];
     CGContextFlush(UIGraphicsGetCurrentContext());
    self.image.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

  }

  last = currentPoint;
  NSLog(@"the current point is %f,%f",currentPoint.x,currentPoint.y);
  NSLog(@"the current point is %f,%f",last.x,last.y);
}
于 2012-11-01T13:03:18.890 回答