xcode iphone touch 点画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5052030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:16:24  来源:igfitidea点击:

xcode iphone touch dotted painting

iphonexcodetouchpaintdotted-line

提问by arvin Arabi

HI every body I'm french so scuse me for my english. My problem is that I want to draw with my finger on the iphone a dotted drawing like that -----------, not a line but a draw.I have :

大家好,我是法国人,所以请教我的英语。我的问题是我想用我的手指在 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();

What is the code for "dotted" please.

请问“dotted”的代码是什么。

回答by zrzka

CGContextSetLineDash

CGContextSetLineDash

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html%23//apple_ref/c/func/CGContextSetLineDash

http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html%23//apple_ref/c/func/CGContextSetLineDash

Example:

例子:

CGFloat dashes[] = { 1, 1 };
CGContextSetLineDash( context, 0.0, dashes, 2 );

Or simply open QuartzDemo sample in Xcode and look at QuartzLines.m file (QuartzDashView class).

或者简单地在 Xcode 中打开 QuartzDemo 示例并查看 QuartzLines.m 文件(QuartzDashView 类)。

You should really read documentation (see already mentioned link).

您应该真正阅读文档(请参阅已经提到的链接)。

回答by fightingshadow

Your problem is you did not reference the context before doing this: CGContextSetLineDash( context, 0.0, dashes, 2 );

您的问题是您在执行此操作之前没有引用上下文: CGContextSetLineDash( context, 0.0, dashes, 2 );

You need to do this: CGContextRef context = UIGraphicsGetCurrentContext();then replace all your UIGraphicsGetC... calls with context, to speed it up anyway.

你需要这样做:CGContextRef context = UIGraphicsGetCurrentContext();然后用上下文替换你所有的 UIGraphicsGetC... 调用,无论如何都要加快速度。

Deitel's iPhone App-Driven Approach book has an example of doing this.

Deitel 的 iPhone App-Driven Approach 一书中有一个这样做的例子。

FightingS

格斗S

回答by alones

See the great page about the roles of line properties! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

请参阅有关线属性角色的精彩页面! https://horseshoe7.wordpress.com/2014/07/16/core-graphics-line-drawing-explained/

According to the above page, here is the code for the 'dot' line like ( . . . .)

根据上面的页面,这里是“点”线的代码,如 ( . . . . )

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