An analysis of Calayer in iOS development

Source: Internet
Author: User

First, UIView and Calayer

First, we look at the inheritance relationship, Uiview->uiresponder->nsobject, and Calayer directly inherit from NSObject, visible in the NSObject tree model, calayer than UIView hierarchy higher, But that doesn't mean anything, so let's take a look at the explanations in Calayer's API documentation.

  • The Calayer class manages image-based content and allows you to perform animations on that content. Layers is often used to provide the backing store for view but can also is used without a view to display content. A layer ' s main job is to manage the visual content of the provide but the layer itself have visual attributes that can be Set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as it s position, size, and transform) that's used to present that content onscreen. Modifying the properties of the layer is a initiate animations on the layer ' s content or geometry. A Layer object encapsulates the duration and pacing of a layer and its animations by adopting the Camediatiming protocol, which defines the layer ' s timing information. If the Layer object is created by a view, the view typically assigns itself as the layer ' s delegate automatically, and Yo u shouldNot a change of that relationship. For layers your create yourself, you can assign a delegate object and use this object to provide the contents of the layer Dynamically and perform other tasks. A layer may also has a layout manager object (assigned to the LayoutManager property) to manage the layout of Subviews SE Parately.

In general, Calayer class management is based on image content and allows you to perform animated content. Layers are often used to provide properties and methods that view does not have, but you can also display content through a view. The primary work of a layer is to manage the visual content you provide, but the layer itself properties can also be set, such as background color, border, and shadow. In addition to managing visual content, layers also have geometry information (such as its location, size, and transformations), which can be displayed by setting the content. Modifying the properties of a layer can be used to set animations based on the layer itself and its position. The Layer object contains the duration of the layer and the animation effect based on the Camediatiming protocol, which defines the timing information for the layer.

If the Layer object is based on View,view, the layer is usually automatically set by proxy, and no modification is necessary. After a layer is created, you can specify a delegate object and use the properties provided by that object to perform other tasks conveniently. Layers also have a layout manager object (assigned to the LayoutManager property) to manage the layout of the child views separately.

I believe you have a preliminary understanding of the layer, each UIView internal default associated with a Calayer, UIView has frame, bounds and center three properties, Calayer also have similar properties, respectively, frame, bounds, Position, Anchorpoint. Below is position, anchorpoint do further explanation.

Ii. position and Anchorpoint

@property cgpoint position @property cgpoint anchorpoint

For example, a A4 of white paper with a Pushpin on the desk, if not very tight, white can be rotated clockwise or counterclockwise around the Pushpin, this time the Pushpins play the role of Fulcrum. We want to explain the anchorpoint is equivalent to the white pin, its main function is used as the pivot point of transformation, rotation is a transformation, similar to the translation, scaling.

Obviously, the rotation of the white paper with the position of the pin is different, the Pushpin is set in the middle of the white paper and the upper left corner, respectively, created two kinds of rotation, which is determined by the position of the Pushpin (Anchorpoint). How to measure the position of Pushpins (Anchorpoint) in white Paper? In iOS, the value of the Anchorpoint point is determined by a relative bounds scale value, in the upper-left corner of the white paper, in the lower-right corner, the Anchorpoint is divided into (0,0), (1, 1), which means that anchorpoint is in the cell coordinate space ( is also defined in the left-hand coordinate system). The center point of the White Paper, the lower-left corner and the upper-right corner of the Anchorpoint are (0.5,0.5), (0,1), (1,0).

Then look at the official definition of position:

    • The layer ' s position in its superlayer ' s coordinate space.

That is, position is the position of the layer relative to the Superlayer coordinate space

In the API's description of frame, there is this sentence:

    • Layers has an implicit frame, which is a function of the position, bounds, anchorpoint, and transform properties.
    • When you specify the frame of a layer, position are set relative to the anchor point. When you specify the position of the layer, bounds are set relative to the anchor point.

Position is the position coordinate of the anchorpoint point in the layer in the Superlayer. Therefore, it can be said that the position point is relatively suerlayer, Anchorpoint point is a relative layer, the two are relatively different coordinate space of a coincident point.

When you set the frame property of a layer, position is determined by the value of the anchor point (Anchorpoint), and when you set the Position property of the layer, bounds is determined by the anchor Point (Anchorpoint).

When you set the frame property of a layer, the position of the position point (that is, the position coordinate) is determined by the value of the anchor point (Anchorpoint), and when you set the Position property of the layer, The position of the bounds (that is, the orgin coordinate of the frame) is determined by the anchor Point (Anchorpoint).

We can get the following conclusions:

1, position is the cgpoint type, bounds is the cgrect type, according to the anchor Point (anchorpoint) to determine only their location.

2, and UIView similar, Calayer also have Superlayer and sublayer, they each have relative coordinate space.

3, relevance: position is used to determine the position in the parent layer, is the anchorpoint of the child layer in the superlayer position coordinates.

4. Cross-specific: individually modifying position and Anchorpoint does not affect another property. Position and Anchorpoint are coincident points in different coordinate spaces, and modifying the position of coincident points in one coordinate space does not affect the position of the coincident point in another coordinate space. If modified individually, only the layer will be moved, and the other coordinates will not change.

An analysis of Calayer in iOS development

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.