Rahul Vyas
Rahul Vyas

Reputation: 28730

How to draw a dotted rectangle marquee on iphone?

How to draw rectangle something like this in iphone sdk using core graphics

Screen Shot of the Crop Tool

Upvotes: 2

Views: 5036

Answers (3)

dlamblin
dlamblin

Reputation: 45351

Specifically if you look at the actual iPhone reference documentation for CGPath You'll find a section about CGPathAddRect. After which you'll probably find the section of the 2D guide on Painting a Path useful.

CGPathAddRect

Appends a rectangle to a mutable graphics path.

void CGPathAddRect (
   CGMutablePathRef path,
   const CGAffineTransform *m,
   CGRect rect
);

Parameters
path
The mutable path to change.

m
A pointer to an affine transformation matrix, or NULL if no transformation is needed. If specified, Quartz applies the transformation to the rectangle before adding it to the path.

rect
The rectangle to add.

Discussion
This is a convenience function that adds a rectangle to a path, using the following sequence of operations:

// start at origin
CGPathMoveToPoint (path, m, CGRectGetMinX(rect), CGRectGetMinY(rect));

// add bottom edge
CGPathAddLineToPoint (path, m, CGRectGetMaxX(rect), CGRectGetMinY(rect));

// add right edge
CGPathAddLineToPoint (path, m, CGRectGetMaxX(rect), CGRectGetMaxY(rect);

// add top edge
CGPathAddLineToPoint (path, m, CGRectGetMinX(rect), CGRectGetMaxY(rect));

// add left edge and close
CGPathCloseSubpath (path);

Availability
Available in iPhone OS 2.0 and later.

Declared In
CGPath.h

Upvotes: 2

NSResponder
NSResponder

Reputation: 16861

I would recommend dispensing with the handles. They make sense when you're using a mouse; with a touch screen, not so much.

Upvotes: 2

Dave DeLong
Dave DeLong

Reputation: 243156

That's five rectangles.

You construct a CGPath for the dotted line rectangle, stroke it, and then construct the four little CGPaths for the handles and stroke those.

Upvotes: 4

Related Questions