画一条直线iOS [英] Drawing a straight line iOS

查看:98
本文介绍了画一条直线iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码绘制一条直线(显然是在用户触摸上),我需要显示从起点直到用户握住触摸(未触摸结束)的直线,就像来自的橡皮筋一样起点,跟随手指移动到哪里。我可能忽略了我需要修改以创建必要效果的东西。任何想法?

I am drawing a straight line using this code (on user touch obviously) and I need to display the straight line from the start point until where the user is holding the touch (not ended the touch) like a rubber band from the start point, following the finger wherever it moves. I might have overlooked something I need to modify to create the necessary effect. Any idea?

注意:我在View Controller中。

NOTE : I am inside a View Controller.

- (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:] method

This is the [drawline frompoint: topoint:] method

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();


推荐答案

DrawingApp 这是我发现的一个示例项目,可以满足需要。

DrawingApp This is a sample project I found which does what was desired.

另一种方法是,正如Lefteris在评论中建议的那样,在UIView中,覆盖触摸开始,移动和结束。并使用drawRect绘制线条。该线将拖动到您当前触摸的位置。

Another approach is, as Lefteris suggested in the comment, inside UIView, override touches began, moved and ended. And use drawRect to draw the line. The line will drag to wherever your current touch is.

这篇关于画一条直线iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆