gpt4 book ai didi

ios - 使用 CoreGraphics 在 iPhone 上绘制简单的线条

转载 作者:技术小花猫 更新时间:2023-10-29 11:10:56 26 4
gpt4 key购买 nike

我想在用户触摸屏幕的位置和触摸结束的位置之间画一条直线。我需要多行,因为如果用户重复触摸拖动释放操作,我还需要一个按钮来清除所有行。到目前为止,我在下面有这段代码,但是一旦再次调用它,我就会收到错误:CGContextSetStrokeColor:无效上下文 0x0。此错误重复出现:CGContextBeginPath、CGContextMoveToPoint、CGContextAddLineToPoint、CGContextDrawPath。

有什么想法吗?

- (void)drawRect:(CGRect)rect {   
c = UIGraphicsGetCurrentContext();

CGFloat black[4] = {0, 0,
0, 1};
CGContextSetStrokeColor(c, black);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 100, 100);
CGContextAddLineToPoint(c, 100, 200);
CGContextStrokePath(c);
}

最佳答案

完整代码如下。

/* Set the color that we want to use to draw the line */ 
[[UIColor brownColor] set];
/* Get the current graphics context */
CGContextRef currentContext =UIGraphicsGetCurrentContext();
/* Set the width for the line */
CGContextSetLineWidth(currentContext,5.0f);
/* Start the line at this point */
CGContextMoveToPoint(currentContext,50.0f, 10.0f);
/* And end it at this point */
CGContextAddLineToPoint(currentContext,100.0f, 200.0f);
/* Use the context's current color to draw the line */
CGContextStrokePath(currentContext);

关于ios - 使用 CoreGraphics 在 iPhone 上绘制简单的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6329908/

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