iOS animation Animation-4-1:calayer

Source: Internet
Author: User

First of all: This is a series of articles, refer to other articles under this topic to help you understand this article.

I. Introduction of Calayer

Calayer as the main object of animation, want to deep understanding of animation, must first understand the Calayer. Calayer is included in the Quartzcore framework, which is a cross-platform framework that can be used in iOS and Mac OS x. The essence of developing animations with core animation is to convert the contents of Calayer into bitmaps for hardware operations.
Calayer in iOS is designed primarily for content presentation and animation operations, and Calayer itself is not included in Uikit, and it cannot respond to events. Since Calayer at the beginning of the design to consider its animation operations, calayer many properties can be modified to form an animation effect, this property is called "Implicit animation properties." However, the modification of the property is not animated for the root layer of uiview, because in many cases the root layer acts more as a container, and if its property changes animate, it will directly affect the sublayer. In addition, UIView's root layer creation work is done entirely by iOS and cannot be recreated, but you can add sublayers to the root layer or remove sublayers.

Ii. Common Properties of Calayer

The following table is a common attribute of Calayer:

1. The nature of implicit property animations is that the changes to these properties implicitly imply the Cabasicanimation animation implementation, and you can refer to the "Animatable Properties" section of the Xcode help document.

2. The Frame property is rarely used in Calayer because the frame itself does not support animation effects and is usually replaced with bounds and position.

Transparency in 3.CALayer uses opacity instead of alpha, and the center point uses position instead of center.

The 4.anchorPoint property is the anchor point of the layer, and the range in (0~1,0~1) represents the scale of the x and Y axes, which can always coincide with the position (center point), and when the center point of the layer is fixed, Adjust the anchorpoint to achieve the adjustment layer display position (because it is always coincident with position)

To further illustrate the role of Anchorpoint, assume that there is a layer size 100*100, now the center point position (50,50), which can be derived from the frame (0,0,100,100). As mentioned above Anchorpoint default is (0.5,0.5), with the center point position coincident, at this time use the graphic description 1, when the change Anchorpoint to (0,0), the anchor point is in the upper left corner, but the center point Poition does not change, So the layer moves to the lower-right corner, 2, and then modifies Anchorpoint to (,), position or hold position, the anchor is in the lower-right corner of the layer, and the layer

I also wrote a demo to help you understand the role of some of the properties.

Code

Import Uikitclass Viewcontroller:uiviewcontroller {@IBOutlet weak var viewforlayer:uiview! var mylayer:calayer{return Viewforlayer. Layer} override func Viewdidload () {Super. Viewdidload() Setuplayer ()/additional setup after loading the view, typically from a nib. } func Setuplayer () {Mylayer. BackgroundColor= Uicolor. Bluecolor(). CgcolorMylayer. BorderWidth= -Mylayer. BorderColor= Uicolor. Redcolor(). CgcolorMylayer. Shadowradius=3Mylayer. Shadowoffset= Cgsize (width:0, Height:3) Mylayer. Shadowopacity=0.7Mylayer. Contents= UIImage (named:"Star")?. CgimageMylayer. Contentsgravity= Kcagravitycenter Mylayer. Magnificationfilter= Kcafiltertrilinear Mylayer. geometryflipped= false} @IBAction func tap (sender:anyobject) {Mylayer. Shadowopacity= Mylayer. Shadowopacity==0.7?0.0:0.7} @IBAction func Pinch (sender:uipinchgesturerecognizer) {Let offset:cgfloat = Sender. scale<1?5.0: -5.0Let Oldframe = Mylayer. FrameLet Oldorgin = Oldframe. OriginLet newSize = Cgsize (width:oldframe. Width+ (OFFSET *-2.0), Height:oldframe. Height+ (OFFSET *-2.0)) Let Neworgin = Cgpoint (x: Oldorgin. x+ Offset,y: Oldorgin. Y+ offset) Let Newframe = CGRect (Origin:neworgin, size:newsize) if Newframe. Width>=100.0&& Newframe. Width< -{Mylayer. BorderWidth-= Offset Mylayer. Cornerradius+ = offset/2.0Mylayer. Frame= Newframe}} override func Didreceivememorywarning () {Super. didreceivememorywarning()//Dispose of any of the resources that can be recreated. }}


iOS animation Animation-4-1:calayer

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.