0
我正在繪製一條使用此代碼的直線(很明顯用戶觸摸),我需要顯示直線從起點直到用戶持有觸摸(不結束觸摸)從起點開始,沿着手指移動的橡皮筋。我可能忽略了某些我需要修改以創建必要的效果。任何想法?畫一條直線iOS
注:我在一個視圖控制器。
- (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:尖山:]方法
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();
如果您嘗試使用該代碼,會發生什麼情況?你的drawLineFromPoint:toPoint方法是做什麼的? –
你忽視了最重要的部分......當手勢移動時實際做了些什麼。現在你只有觸摸開始和結束時的邏輯。 – borrrden
我已經嘗試觸動移動部分中的setNeedsDisplay。沒有結果。 – esh