The best way to implement drawing features like Keynote

I'm trying to make a small iPad tool for drawing simple geometric objects (rectangle, rounded rectangle, ellipse, star, ...). My goal is to do something very close to Keynote (the drawing function), that is, let the user add a rectangle (for example), resize it and move it. I also want the user to be able to select many objects and move them together.

I was thinking of at least 3 different ways to do this:

  • Extends UIView for each type of object, class for Rect, another for Ellipse, ... using a custom drawing method. Then add this view as a subview of the global view.
  • Extends CALayer for each type of object, class for Rect, another for Ellipse, ... using a custom drawing method. Then add this layer as a sublayer of the global view layer.
  • Extends NSObject for each type of object, a class for Rect, another for Ellipse, ... Using just a drawing method, which takes CGContext and Rect as an argument and draws the form directly in it. These methods will be called by drawing a global view.

I know that the first two ways come with functions for detecting the touch of each object, for adding easily shadows ... but I'm afraid that they are too heavy? That is why I thought of the last method, which seems straightforward.

Which way will be more efficient? Or maybe I did not think otherwise?

Any help would be appreciated -)

Thanks.

+6
optimization iphone calayer uiview drawing
source share
4 answers

I would use the UIKit classes to make your drawing and then profile and optimize your code.

Apple / iPad Information: Link Text

+1
source share

My first feeling was to take the third path, but to be sure, right after I sent my message, I did some tests with a global look and more than 200 geometric shapes (Rect, Rounded Rect and Ellipse) on he and I only move half with the touchMoved event. I tested this test using method 1 (subclassing UIView) and method 3 (subclassing NSObject), path 2 seems to me too restrictive and does not help me at all. Resuslt is that method 1 seems more efficient ... There is no lag when I move 60 objects together! Moreover, using this method will probably help me, because when using the view, some interesting function arises, such as touch detection on a complex path (see UIBezierPath), a hierarchy of objects processed by the UIView shell ...

So, I will use this path and come back here to share my results; -)

Hi

0
source share

It is better to use CGLayer objects. Benefits:

  • It is much faster and more efficient. For simple objects, adding a view is much more expensive and complicates the hierarchy of views. For complex objects, caching performed on CGLayers can improve performance.

  • Easily group objects together. you just put everything in a new layer, and voila! There is almost no overhead.

  • Using CGLayer and other Quartz objects gives you great flexibility.

The only drawback is that you must use Quartz 2D directly. It is not very difficult, but you need to learn if you have not used it before.

0
source share

CAShapeLayer pretty much handles your option 2. The default is a straight and rounded rectangle (see cornerRadius), or you can give it a path for any arbitrary shape. For your option 1, you can use CAShapeLayer with a UIView instead of a drawRect implementation, and this can be faster.

0
source share

All Articles