Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

588

CHAPTER 16: Drawing with Quartz and OpenGL

CGRect with a negative size will simply be rendered in the opposite direction of its origin point (to the left for a negative width; upward for a negative height).

CGRect currentRect = CGRectMake(firstTouch.x, firstTouch.y,

lastTouch.x - firstTouch.x, lastTouch.y - firstTouch.y);

Once we have this rectangle defined, drawing either a rectangle or an ellipse is as easy as calling two functions: one to draw the rectangle or ellipse in the CGRect we defined, and the other to stroke and fill it.

case kRectShape:

CGContextAddRect(context, currentRect); CGContextDrawPath(context, kCGPathFillStroke); break;

case kEllipseShape: CGContextAddEllipseInRect(context, currentRect); CGContextDrawPath(context, kCGPathFillStroke); break;

Compile and run your application. Try out the Rect and Ellipse tools to see how you like them. Don’t forget to change colors, including using a random color.

Drawing the Image

For our last trick, let’s draw an image. The 16 - QuartzFun folder contains an image named iphone.png that you can add to your Supporting Files folder, or you can use any

.png file you prefer, as long as you remember to change the file name in your code to point to that image.

Add the following code to your drawRect: method:

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);

CGContextSetStrokeColorWithColor(context, currentColor.CGColor);

CGContextSetFillColorWithColor(context, currentColor.CGColor); CGRect currentRect = CGRectMake(firstTouch.x,

firstTouch.y,

lastTouch.x - firstTouch.x, lastTouch.y - firstTouch.y);

switch (shapeType) { case kLineShape:

CGContextMoveToPoint(context, firstTouch.x, firstTouch.y); CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y); CGContextStrokePath(context);

break;

case kRectShape:

CGContextAddRect(context, currentRect); CGContextDrawPath(context, kCGPathFillStroke); break;

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]