IOS Calayer (ii)

Source: Internet
Author: User

There is a default Calayer object layer inside the UIView, although we cannot recreate it, but we can add a sub-layer on top of the door.

We know that UIView has Addsubview: methods, similarly, Calayer also have Addsublayer: methods. Our door can be added from the layer by Addsublayer: again there are layers.

Below we show how to add self-layer.

  viewcontroller.m//  Cx-calayer (ii)////  Created by Ma C on 16/3/19.//  Copyright 2016 Xubaoaichiyu . All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void ) Viewdidload {    [super viewdidload];    Initialize layer    calayer * layer = [[Calayer alloc]init];    Set Layer frame    layer.frame = CGRectMake (0, Self.view.frame.size.width, max);    Sets the background color of the layer    Layer.backgroundcolor = [Uicolor Orangecolor]. Cgcolor;    Add layer on Self.view.layer    [Self.view.layer addsublayer:layer];        } @end

In addition to this simple layer, we can also set the layer of the picture. (layer.contents = (ID) [UIImage imagenamed:@ "Nvshen.jpg"]. Cgimage;)

  viewcontroller.m//  Cx-calayer (ii)////  Created by Ma C on 16/3/19.//  Copyright 2016 Xubaoaichiyu . All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void ) Viewdidload {    [super viewdidload];    Initialize layer    calayer * layer = [[Calayer alloc]init];    Set Layer frame    layer.frame = CGRectMake (0, Self.view.frame.size.width, self.view.frame.size.height);    Add picture    layer.contents = (ID) [UIImage imagenamed:@ "Nvshen.jpg"]. Cgimage;    Sets the background color of the layer    Layer.backgroundcolor = [Uicolor Orangecolor]. Cgcolor;    Add layer on Self.view.layer    [Self.view.layer addsublayer:layer];        } @end

The following is a reference to MJ God's explanation:

Why use the 2 data types Cgcolorref and Cgimageref in Calayer instead of Uicolor and uiimage?

* First of all to know: Calayer is defined in the Quartzcore framework, CGIMAGEREF, cgcolorref two data types are defined in the Coregraphics framework; Uicolor, UIImage is defined in the Uikit frame

* Second, the Quartzcore framework and the Coregraphics framework are available across platforms and can be used on both iOS and Mac OS x, but Uikit can only be used in iOS

* Therefore, in order to ensure portability, quartzcore can not use UIImage, Uicolor, can only use Cgimageref, Cgcolorref

* However, in many cases, the Coregraphics object can be obtained by Uikit the specific method of the object, such as UIImage Cgimage method can return a cgimageref

Choice of UIView and Calayer

* In fact, compared with the Calayer,uiview more than one event processing function. In other words, Calayer cannot handle user touch events, and UIView can

* So, if the displayed things need to interact with the user, with UIView, if you do not need to interact with the user, with UIView or Calayer can

* Of course, calayer performance will be higher because it is less event-handling, more lightweight

Other relationships between UIView and Calayer

* UIView can access all sub-views through the Subviews property, similarly, Calayer can access all child layers through the Sublayers property

* UIView can access the parent view through the Superview property, similarly, Calayer can also access the parent layer through the Superlayer property

* See below for a diagram of UIView and Calayer:

If two UIView is a parent-child relationship, then the Calayer within them is also a parent-child relationship.

IOS Calayer (ii)

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.