What are the differences between a UIView and a CALayer?

IosUiviewUikitCalayer

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

On iOS, every UIView is backed by a Core Animation CALayer, so you are dealing with CALayers when using a UIView, even though you may not realize it. Unlike NSViews on the Mac, which evolved before Core Animation existed, UIViews are intended to be lightweight wrappers around these CALayers.

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

One thing UIViews provide over CALayers is built-in support for user interaction. They handle hit-testing on touches and other related actions that you would need to build yourself if managing a hierarchy of CALayers. It's not that hard to implement this yourself, but it is extra code you'd need to write when building a CALayer-only interface.

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

Solution 2 - Ios

From the Ray Wenderlich blog (Tutorial)

> CALayers are simply classes representing a rectangle on the screen > with visual content. “But wait a darn minute,” you may say, “that’s > what UIViews are for!” That’s true, but there’s a trick to that: > every UIView contains a root layer that it draws to!

Solution 3 - Ios

Simply speaking,UIView inherit from UIResponder, handle events from users, contains CALayer, which inherit from NSObject, mainly focus on rendering, animation etc.

Solution 4 - Ios

UIView is a container for CALayers. Using UIKit.

CALayer where we draw the contents. Using CoreGraphics

If you work with custom control like features it would be great to go ahead with single view containing more layers for accurate native rendering. Since CALayers are weightless than UIView.

To create common skeleton for Mac and iOS, follow the design for your app using CALayers. Since it is available in both platform.

UIView having feature like touch events achieved using delegates -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event, tochesStart like events and other UIKit features.

To work with CALayers use Core Graphics knowledge.For any simple view rendering UIView is enough.

Solution 5 - Ios

UIView: Views have more complex hierarchy layouts. They can receive user interactions like taps, pinches, cliks and more. Working with UIViews happens on the main thread, it means it is using CPU power.

CALayer: Layers on other hand have simpler hierarchy. That means they are faster to resolve and quicker to draw on the screen. There is no responder chain overhead unlike with views. Layers are drawn directly on the GPU. It happens on a separate thread without burdening the CPU.

For more details: https://medium.com/@fassko/uiview-vs-calayer-b55d932ff1f5

Solution 6 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleg DanuView Question on Stackoverflow
Solution 1 - IosBrad LarsonView Answer on Stackoverflow
Solution 2 - IosMatView Answer on Stackoverflow
Solution 3 - IosForrestView Answer on Stackoverflow
Solution 4 - IosGobi MView Answer on Stackoverflow
Solution 5 - IosAnit KumarView Answer on Stackoverflow
Solution 6 - IosmusixlemonView Answer on Stackoverflow