1

I'm learning Cappuccino Objective J, having a little difficulty understanding the difference between CPView, CALayer, CPImageView when it comes to drawing.

I have a good knowledge of JavaScript, but no experience with Objective C & cocoa.

I'm building a small app for my uni project which will require drag & drop of images, drawing lines and other shapes to link them. An app that does something like this is gomockingbird.

My question is which of the mention above should i used to drop and draw on. I've look at the FloorPlan example on the cappucino.org website and it uses CPImageView object to drag and drop items on. Since i read from a tutorial that CALayer is optimized for this type of operation, I'm having a hard time knowing which one to use.

Any links to examples or tutorials are welcome.

Thanks.

1 Answers1

1

In general, CALayers are old and shouldn't be used.

CPViews are the most basic of UI elements on the screen. Just about everything is backed by a view (windows, image views, buttons, etc). Views can have any number of subviews, and a single superview.

If you want custom drawing in a view (that is, CoreGraphics or canvas like APIs) you need to subclass UIView or a decedent of UIView. You will then override

- (void)drawRect:(CGRect)aRect;

UIImageView is a subclass of UIView. It's only purpose is to display an image on the screen.

For your project, I think I would do the following: Create a "canvas view" which is what the user will be interacting with. Drag and drop UIImageViews onto the canvas as needed. Then for the line drawing, have the canvas view itself draw lines from each image.

It's important to remember that large views that do custom drawing tend to become slow. If you notice some slowness as the user is drawing, it might make sense to insert a temporary draw view under all the images. Then when the user drops drawing lines, the "canvas view" updates itself and the temporary drawing view is removed from the view hierarchy.

Hope that helps a bit!

Me1000
  • 1,760
  • 1
  • 12
  • 14
  • If CALayers aren't supposed to be used why does the tutorial on the cappuccino site use layers and affine transforms to implement scaling and rotation ? Can it be done any other way ? I think you meant CPImageView and not UIImageView . – Samhan Salahuddin Nov 05 '12 at 04:27
  • As, sorry you're right… I did mean CPImageView. Views cannot be rotated out of the box, only layers. In Cocoa land, people go to layers for performance reasons… in Cappuccino land, layers will probably be slower than views, so generally you wouldn't use them for only performance reasons. Transforms would be a good reason to use them though. – Me1000 Nov 07 '12 at 20:54
  • Thanks for the info and advice. I do need the ability to to resize my "canvas view" but i don't require transformations. Should i subclass CPView or CALayer? Sorry for the noob questions, objective j examples are hard to come by or I'm probably looking in the wrong places. Regards – IslandCoder Nov 07 '12 at 22:06
  • Just subclass CPView such that it knows how to display the content with any arbitrary size. Otherwise, the CPView should resize itself such that it can always display everything inside of it. This would assume you always place it in a scrollview. – Me1000 Nov 08 '12 at 02:50