[iOS Animation]-calayer Layer Tree Two

Source: Internet
Author: User
<span id="Label3"></p><span id="OSC_h2_1"><span id="OSC_h2_1"></span></span>The ability of the layer<p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">If Calayer is the internal implementation detail of uiview, then why should we understand it all in all? Apple certainly provides us with a graceful and concise uiview interface, so is there no need to deal directly with the details of the core animation?</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">In a sense, it is true that we do not need to deal with Calayer for some simple needs, because Apple has indirectly made the animation simple by Uiview's advanced Api.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">But this simplicity inevitably leads to some flexible flaws. If you slightly want to make some changes at the bottom, or use some of the interface features that Apple does not implement on uiview, then there is no choice but to intervene in the core Animation.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">We have confirmed that a layer cannot handle touch events like a view, so what views can he do that cannot be done? Here are some UIView features that are not exposed by calayer:</p></p> <ul> <ul> <li>shadows, rounded corners, colored borders</li> <li>3D transformations</li> <li>Non-rectangular Range</li> <li>Transparent matte</li> <li>Multi-Stage Nonlinear Animation</li> </ul> </ul> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">WE'LL Explore these features in the next chapters, and first we'll look at how Calayer is used in the Application.</p></p> <span id="OSC_h2_2"><span id="OSC_h2_2"></span></span>Working with Layers<p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Let's start by creating a simple project to manipulate the properties of some layer. Open Xcode and create a project using the single <em>View application</em> Template.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Create a small view in the center of the screen (about 200 X size), Of course you can code it manually, or use interface Builder (handy). Make sure that your view controller adds a View's properties so that it can be accessed directly. We call it the LAYERVIEW.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Run the project, you should be able to see a white square in the light gray screen background (figure 1.3), if not seen, you may need to adjust the background window or view color</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"></p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Fig. 1.3 A white uiview on a gray background</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">There's Nothing exciting about this, Let's add a color block and add a small blue block in the middle of the white Square.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">We can of course simply add a Sub-view (arbitrary code or Ib) to an already existing uiview, but that doesn't really learn anything about Layers.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">So let's create a calayer and use it as a sublayer for the layers that are related to our VIEW. Although layer properties are exposed in the interfaces of the UIView class, the standard Xcode project template does not contain a core animation related header File. So if we do not add the appropriate library to the project, we are not able to use any layer-related methods or access its Properties. So first you need to add the Quartzcore framework to the build phases tag (figure 1.4), and then introduce the library in the vc's. m file.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"></p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Figure 1.4 Adding the Quartzcore library to the project</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">You can then directly reference the properties and methods of Calayer in your Code. In Listing 1.1, we created a calayer, set its backgroundcolor property, and then added to the sublayer of the related layer behind Layerview (this code is based on the creation of Layerview with IB and is connected), and figure 1.5 shows the Result.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Listing 1.1 Adding a blue sublayer to the view</p></p> <table border="0" cellpadding="0" cellspacing="0" style="border:1px solid silver;width:730px;margin:0px !important;padding:0px !important;height:auto !important;font-family:Consolas, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, Courier, monospace !important;font-size:12px !important;background:none !important;"> <tbody> <tr> <td style="border:1px solid silver;vertical-align:baseline !important;font-family:Consolas, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, Courier, monospace !important;color:#AFAFAF !important;background:none !important;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21</td> <td style="border:1px solid silver;vertical-align:baseline !important;font-family:Consolas, ‘Bitstream Vera Sans Mono‘, ‘Courier New‘, Courier, monospace !important;background:none !important;"></td> </tr> </tbody> </table> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"></p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Figure 1.5 White UIView inner nested blue Calayer</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">One view has only one associated layer (created automatically), and it can also support adding countless sublayers, as shown in Listing 1.1, you can show that you create a separate layer and add it directly to the sublayer of the View's associated Layer. Although it's possible to add layers like this, often we just see that the views are simply processed, and the layers they're associated with don't need to be added to the extra layers manually.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">Prior to version 10.8 of the Mac OS platform, a significant performance flaw was the use of the Calayer tree hierarchy in a view hierarchy instead of a separate one. But on the iOS platform, the use of the lightweight UIView class has no significant performance impact (of course, after Mac OS 10.8, Nsview's performance has also been greatly improved).</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">The advantage of using a layer's associated view instead of Calayer is that you can use Uiview's Advanced APIs (such as automatic typesetting, layout, and event Handling) while using all of the calayer underlying Features.</p></p> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">however, you may need to use Calayer instead of UIView when the following conditions are Met:</p></p> <ul> <ul> <li>Develop Cross-platform applications that can run on Mac OS at the same time</li> <li>Use subclasses of multiple Calayer (see Chapter sixth, "special Layers") and do not want to create additional uiview to package them all</li> <li>Doing something that is particularly critical to performance, such as some negligible operations on uiview, can make a difference (though you might want to use OpenGL drawing Directly)</li> </ul> </ul> <p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">however, These examples are rare, and in general, processing views is more convenient than working with layers Alone.</p></p> <span id="OSC_h2_3"><span id="OSC_h2_3"></span></span>Summarize<p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;"><p style="font-family:verdana, ‘ms song‘, 宋体, Arial, 微软雅黑, Helvetica, sans-serif;font-size:12px;background-color:#FFFFFF;">This chapter describes the tree structure of the layer, explaining how a parallel calayer hierarchy is formed in iOS by the hierarchical relationship of uiview, and in subsequent experiments we create our own calayer and add it to the layer Tree.</p></p><p><p>[iOS Animation]-calayer Layer Tree Two</p></p></span>

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.