Use of the IOS layer (shadow)

Source: Internet
Author: User

Calayer (layer) is a rectangular area on the screen that contains a root calayer in each UIView, and all visual effects on the UIView are performed on this layer.

Calayer profile features include:

1, the size of the layer size

2. Background color

3, content (can fill the picture or use the core graphics to draw content)

4. Whether the rectangle uses rounded corners

5. Whether the rectangle has a shadow

There are many kinds of layers, the most common is the most basic is Calayer, of course, including other sub-categories:

Cascrollerlayer part of a simplified display layer

Catextlayer text Layer

Cagradientlayer, Cashapelayer, etc.

Before using a layer, you need to introduce the Quartzcore.framework framework into your project

Instance:

New Xcode project Layersample, import quartzcore.famework

Introduced in the LayerSampleViewController.h file:

#import <QuartzCore/QuartzCore.h>
LAYERSAMPLEVIEWCONTROLLER.M file, code:
"LayerSampleViewController.h" @implementation layersampleviewcontroller-(void) Loadview {    UIView *rootview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 1024x768, 768)];
    Rootview.backgroundcolor = [Uicolor Whitecolor];
    Self.view = Rootview;    [Rootview release];    //Load layer    Backlayer = [Calayer layer];    Backlayer.backgroundcolor = [Uicolor Orangecolor]. Cgcolor;    //Set the area    of the layer //Set layer coordinates    [Self.view.layer Addsublayer:backlayer];} -(void) dealloc {    [Super Dealloc];} @end   

To run the program:

To distinguish the background uiview, I set the background color of the uiview to white, and the background color of the backlayer is set to orange.

is a rectangular area, you can add properties if the four corners of the set rectangle are elliptical corners:

Backlayer.cornerradius = 20.0; //radius of curvature of the rectangular oval corner

After running the program, the effect

To add a shaded child layer (layer), code:

     Add a child layer    calayer *cyanlayer = [Calayer layer];    Cyanlayer.backgroundcolor = [Uicolor Cyancolor]. Cgcolor;    Cyanlayer.bounds = CGRectMake (0, 0, +);    Cyanlayer.position = Cgpointmake (in.);    //Set the offset of the shadow    Cyanlayer.shadowradius = 10.0;  //Set the radius    of the shadow //Set the shadow color to black    //Set the opacity of the    shadow [Backlayer Addsublayer:cyanlayer];   

Run:

Add a picture to a child layer

Add a child image layer

    UIImage *image = [UIImage imagenamed:@ "feiche.jpg"];    Calayer *imagelayer = [Calayer layer];    Imagelayer.frame = CGRectMake (Image.size.width, image.size.height);    Imagelayer.contents = (ID) image. Cgimage;    //Set the offset of the shadow    Imagelayer.shadowradius = 10.0;  //Set the radius    of the shadow //Set the shadow color to black    //Set the opacity of the    shadow [Backlayer Addsublayer:imagelayer];    

Operation Effect,

Picture plus border, code:

Imagelayer.bordercolor = [Uicolor Graycolor]. Cgcolor;  //Border color imagelayer.borderwidth = 2.0;  Border Width

Operating effect:

To set the image to an elliptical angle, code:

Imagelayer.cornerradius = 10.0;  //Set layer fillet radius imagelayer.maskstobounds = YES;  Hide Boundary

Operating effect:

However, because the set Maskstobounds property is true, the shadow effect on the layer is not there.

Before I was the picture in addition to make a shadow effect, so the more realistic, if the shadow part is just fill the border, you can use two layers to achieve the shadow effect, code:

UIImage *image = [UIImage imagenamed:@ "Feiche.jpg"];Shadow layer Calayer *shadowlayer = [Calayer layer];    Shadowlayer.frame = CGRectMake (Image.size.width, image.size.height); Shadowlayer.backgroundcolor = [Uicolor Bluecolor].    Cgcolor;    Shadowlayer.shadowoffset = Cgsizemake (0, 3);    Shadowlayer.cornerradius = 10.0;    Shadowlayer.shadowradius = 10.0; Shadowlayer.shadowcolor = [Uicolor blackcolor]. Cgcolor;//Set the color of the shadow to black shadowlayer.shadowopacity = 1.0;        //Set the opacity of the shadow [Backlayer Addsublayer:shadowlayer];    //Add Sub image layer Calayer *imagelayer = [Calayer layer]; Imagelayer.frame = CGRectMake (Image.size.width, image.size.height); Imagelayer.contents = (ID) image. Cgimage; Imagelayer.cornerradius = 10.0; //Set layer fillet radius imagelayer.maskstobounds = YES; //Hide Boundary Imagelayer.bordercolor = [Uicolor Graycolor]. Cgcolor; //Border color imagelayer.borderwidth = 2.0; [Backlayer Addsublayer:imagelayer];     

Post-run Effects:

Original address: http://blog.csdn.net/liulushi_1988/article/details/9030141

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.