gpt4 book ai didi

ios - 画一条直线 iOS

转载 作者:行者123 更新时间:2023-11-28 20:30:04 26 4
gpt4 key购买 nike

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

注意:我在 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:]方法

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这是我发现的一个示例项目,它可以完成所需的工作。

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

关于ios - 画一条直线 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12527482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com