ios:quartz2d Drawing (drawing shadows for graphics)

Source: Internet
Author: User

Quartz2d can either draw the original shape or draw a shadow to the original.

Some of the parameters you need when drawing shadows: Context, shadow offset, shadow blur factor

Note: Before drawing shadow processing for a drawing, the context needs to be saved before the shadow is drawn successfully, and the context is reset. The purpose is to not affect the subsequent drawing operations.

Examples of shadow drawing examples are as follows:

1. Customize a view class DemoView, and associate the view of the controller with the custom class, and override it in the definition class -(void) DrawRect: (cgrect) Rect, writes a call method that draws no shadow shape and draws a shadow shape.

// calling methods for drawing no shadow shapes and drawing shadow shapes

-(void) DrawRect: (cgrect) rect{     / / Draw graphics      //1. Get the context of the drawing      Cgcontextref context = uigraphicsgetcurrentcontext ();          // draw a shaded rectangle      [self drawrectwithshadow:context];          // draw a rectangle without a shadow      [self drawrectwithnoshadow:context];    }

2, the specific drawing code is as follows:

// Draw a rectangle without shading

#pragma mark-Draw a rectangle with no shadows

-(void  ) Drawrectwithnoshadow: (    CGCONTEXTREF) context{ 3. Adding a rectangle  Cgcontextaddrect (Context, CGRectMake (100 , 300 , 100 , 100              // 4. Setting drawing Properties  [[Uicolor Redcolor]      Setfill]; //     fill color   [[Uicolor Blackcolor]setstroke]; //  stroke  // 5. Draw Rectangle   Cgcontextdrawpath (context, Kcgpatheofillstroke);}  

The shaded rectangles are drawn as:

// draw a shaded rectangle

#pragma mark-Draw a shaded rectangle (shaded: no color (default), colored (set color))

-(void) Drawrectwithshadow: (cgcontextref) context{//Save the old drawing contextcgcontextsavegstate (context); //2. Set Shadow (Parameters: Context, shadow offset, Shadow blur factor)//shadow with no color//Cgcontextsetshadow (Context, Cgsizemake (10, 10), 10.0); //Shaded with colorCgcontextsetshadowwithcolor (Context, Cgsizemake (Ten,Ten),10.0, [[Uicolor Purplecolor]cgcolor]); //3. Add a rectangleCgcontextaddrect (Context, CGRectMake ( -, -, -, -)); //4. Setting Drawing Properties[[Uicolor Redcolor]setfill];//Fill Color[[Uicolor Blackcolor]setstroke];//Strokes//5. Draw a rectangleCgcontextdrawpath (context, kcgpatheofillstroke); //restore the old drawing contextcgcontextrestoregstate (context);}

Draw a shadow graphic with no color and a shaded graphic with color as:

ios:quartz2d Drawing (drawing shadows for graphics)

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.