4

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 :

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.

arvin Arabi
  • 253
  • 1
  • 7
  • 21

3 Answers3

4

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).

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

zrzka
  • 20,249
  • 5
  • 47
  • 73
  • yes but how can i put this in my code please. I'm trying but it doesn't work. – arvin Arabi Feb 20 '11 at 11:25
  • 1
    I just edited my answer - example added. If you're trying to do it by yourself, it's good practice to paste your code here too, so, people can tell (= help) you what's wrong. – zrzka Feb 20 '11 at 14:45
  • xcode telle me context undeclared, but I don't gnow I to solve this problem. – arvin Arabi Feb 20 '11 at 17:19
2

Your problem is you did not reference the context before doing this: 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.

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

FightingS

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
0

See the great page about the roles of line properties! 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);
alones
  • 2,848
  • 2
  • 27
  • 30