我正在使用此代码绘制一条直线(显然在用户触摸时),我需要从起点显示直线,直到用户从起点开始触摸(未结束触摸),就像橡皮筋一样,跟随手指移动。我可能忽略了一些我需要修改以产生必要效果的东西。任何想法?
注意:我在视图控制器内。
- (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;
}
}
这是 [drawline frompoint: 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();