What are the differences between UIView and CALayer?

Both have most of the same attributes, both support different kinds of animations, both represent different data. What are the differences between UIView and CALayer?

+80
ios iphone ipad calayer uiview
Oct 19 '11 at 18:36
source share
5 answers

On iOS, every UIView is supported by CALayer Core Animation, so you use CALayers when using UIView, even if you don't understand it. Unlike NSViews on the Mac, which evolved to Core Animation, UIViews had light wrappers around these CALayers.

As I describe in a similar question, "When to use CALayer on Mac / iPhone?" , working with CALayers does not give you significant performance advantages of UIViews. One of the reasons you might need to create a user interface element with CALayers instead of UIViews is that it can be easily ported to Mac. UIViews are very different from NSViews, but CALayers are almost identical on two platforms. This is why the Core Plot framework exposes its graphs using CALayers instead of other user interface elements.

One thing that UIViews provides over CALayers is the built-in user experience support. They handle hit tests and other related activities that you will need to create yourself if you manage the CALayers hierarchy. It is not so difficult to implement this on your own, but this is additional code that you need to write when creating the CALayer interface.

You will often need access to the base layers for the UIView when performing more complex animations than the UIView base class allows. UIView's animation capabilities have grown as the iOS SDK ripens, but there are a few more things that are best done by interacting with the base CALayer.

+187
Oct 19 '11 at 20:08
source share

From the blog Ray Wenderlich ( Tutorial )

CALayers are simply classes representing a rectangle on the screen with visual content. β€œBut wait a minute,” you say, β€œthat's what UIView is for!” It’s true, but it’s a trick: each UIView contains the root layer that it refers to!

+34
Oct 19 '11 at 18:41
source share

Simply put, UIView inherits from NSResponder, handles events from users, contains CALayer, which inherit from NSObject, mainly focus on rendering, animation, etc.

+22
Dec 12 '13 at 7:33
source share

UIView is a container for CALayers . Using UIKit .

CALayer , where we draw the content. Using CoreGraphics

If you are working with a custom control as a function, it would be great if one view contained more layers for accurate native rendering. Since CALayers weightless than UIView .

To create a common skeleton for Mac and iOS, follow the design of your application using CALayers . Since it is available on both platforms.

UIView having functions such as touch events received using delegates -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event , tochesStart as events and other UIKit functions.

To work with CALayers , use knowledge of Core Graphics. For any simple representation of rendering, UIView enough.

+6
Sep 15 '15 at 12:09
source share

The big difference in UIView is for CocoaTouch on a mobile device. It adds some event handler that CALayer did not provide.

0
May 26 '15 at 3:25
source share



All Articles