Xcode iPhone触摸点画图技术

4

大家好,我是法国人,我的英语可能不太好,请见谅。我的问题是我想在iPhone上用手指画一个像这样的点线图形———,不是一条直线,而是一种图案。
我已经有以下内容:

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

请问"dotted"的代码是什么?
3个回答

4

是的,但我该如何将其放入我的代码中呢?我正在尝试,但它不起作用。 - arvin Arabi
1
我刚刚编辑了我的回答 - 添加了示例。如果你正在尝试自己做,把你的代码粘贴在这里也是一个好的练习,这样人们就可以告诉(=帮助)你哪里出了问题。 - zrzka
Xcode告诉我上下文未声明,但我不知道如何解决这个问题。 - arvin Arabi

2
您的问题是在执行以下操作之前没有引用上下文:CGContextSetLineDash(context,0.0,dashes,2);
您需要执行以下操作:CGContextRef context = UIGraphicsGetCurrentContext();然后将所有的UIGraphicsGetC...调用替换为上下文,以加快速度。
Deitel的iPhone应用驱动方法书中有一个示例可以执行此操作。
FightingS

0

查看关于线条属性角色的优秀页面! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

根据上述页面,以下是“点”线的代码(. . . .)

// should
CGContextSetLineCap(context, kCGLineCapRound);

// please see the role of line properties why the first should be 0 and the second should be the doulbe of the given line width
CGFloat dash[] = {0, lineWidth*2};

// the second value (0) means the span between sets of dot patterns defined by dash array
CGContextSetLineDash(context, 0, dash, 2);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接