Ios development diary 12-CALayer and UIView, 12-calayeruiview

Source: Internet
Author: User

Ios development diary 12-CALayer and UIView, 12-calayeruiview

Today, the blogger has a requirement to draw content and has encountered some difficulties. I would like to share with you the hope that we can make common progress.

Today I will share with you the differences between CALayer and UIView:

1. UIKit uses UIResponder as the response object to respond to and process the events passed by the system. UIApplication, UIViewController, UIView, and all the UIKit classes derived from UIView (including UIWindow) are directly or indirectly inherited from the UIResponder class.

The UIResponder defines interfaces for processing various events and passing events, while CALayer directly inherits NSObject and does not have corresponding interfaces for processing events, the most obvious difference between the two is that the View can accept and process events, but the Layer cannot.

2. A Layer frame is determined by its anchorPoint, position, bounds, and transform. A View frame simply returns the Layer frame, the center and bounds of the same View also return some Layer attributes.

When the View is displayed, the UIView is used as the CALayerDelegate of the Layer. The display content of the View is drawn by the internal CALayer.

3. Each UIView has a CALayer inside it to provide content rendering and display, and the UIView size style is provided by the internal Layer. Both of them have a tree-level structure. The layer has SubLayers and the View has SubViews. However, the Layer has more AnchorPoint than the View.

Layer maintains a three-part layer tree, namely, presentLayer Tree (animation Tree), modeLayer Tree (model tree), and Render Tree (rendering Tree). During iOS animation, we modify the animation attributes. In fact, the animation is actually the attribute value of the Layer presentLayer, and the final display on the Interface actually provides the modelLayer of the View.

4. CALayer supports implicit animation by default. When the Layer of the UIView is animated, the View acts as the proxy of the Layer. The Layer uses actionForLayer: forKey: request the corresponding action (Animated action) to the View. For each UIView, there is a layer, which is also called RootLayer, rather than the root layer of the View. We do not generate default animation when modifying the properties of UIView, but directly modify the properties of individual layers. The default animation time is 0.25 s.

5. I will share with you some tips on converting UIView to UIImage.

+ (UIImage*) imageWithUIView:(UIView*) view{  UIGraphicsBeginImageContext(view.bounds.size);  CGContextRef currnetContext = UIGraphicsGetCurrentContext();  [view.layer renderInContext:currnetContext];  UIImage* image = UIGraphicsGetImageFromCurrentImageContext();  UIGraphicsEndImageContext();  return image;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.