私はこのコードを使用して直線を描いています(明らかにユーザータッチで)。開始点からユーザーが開始点から輪ゴムのようにタッチを保持する(タッチを終了しない)までの直線を表示する必要があります。それが動くところはどこでも指をたどります。必要な効果を出すために修正する必要があるものを見落としているかもしれません。何か案が?
注:私はViewControllerの内部にいます。
- (void) drawLine:(UIPanGestureRecognizer *)sender
{
NSLog(@"Sender : %@", sender);
CGPoint currentPoint;
//CGAffineTransformMakeScale(lastScale, lastScale);
if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateBegan || [(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged)
{
currentPoint = [(UIPanGestureRecognizer *)sender locationInView:imgView];
previousPoint = currentPoint;
}
if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged)
{
touchedPoint = [(UIPanGestureRecognizer *)sender locationInView:imgView];
[imgView setNeedsDisplay];
}
if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded)
{
currentPoint = [(UIPanGestureRecognizer *)sender locationInView:imgView];
imgView.image = [self drawLineFromPoint:previousPoint toPoint:currentPoint image:imgView.image];
previousPoint = currentPoint;
}
}
これは[drawlinefrompoint:topoint:]メソッドです
UIGraphicsBeginImageContext(imgView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, imgView.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetShouldAntialias(context, YES);
CGContextSetLineWidth(context, 1.0f);
CGContextSetRGBStrokeColor(context, 0.7, 0.7, 0.7, 1.0);
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
CGContextDrawPath(context, kCGPathStroke);
CGContextRestoreGState(context);
NSLog(@"frompoint.x : %f, frompoint.y : %f",fromPoint.x, fromPoint.y );
NSLog(@"topoint.x : %f, topoint.y : %f", toPoint.x, toPoint.y);
UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();