xcode iphone touch dotted painting

5.8k views Asked by At

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.

3

There are 3 answers

3
zrzka On

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

0
fightingshadow On

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

0
alones On

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