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 に答える