IOS development (83) to add shadows to shapes

Source: Internet
Author: User

1 Preface
The CGContextSetShadow process is used to apply a shadow to the shape drawn in the graphic environment.


CGContextSetShadowWithColor process: the parameters accepted in this process are identical to those of CGContextSetShadow, but a CGColorRef parameter is added to set the color of the shadow.


2. code example
ZYViewControllerView. m

 

[Plain]-(void) drawRect :( CGRect) rect {
[Self drawRectAtTopOfScreen];
// Since the first rectangle has a shadow, the shadow still exists even if the second rectangle is not set.
[Self drawRectAtBottomOfScreen];
}
 
-(Void) drawRectAtTopOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
// Set the background color in gray
/* Second parameter: the shadow displacement, specified by the CGSize type value, starting from the right lower part of the shadow to be applied in each shape. The larger the x value of the displacement, the shape
The shadow on the right spreads farther and farther. The larger the y value of the displacement, the lower the shadow.
The third parameter is the shadow blur value, which is specified by the floating point value (CGFloat. If 0.0f is specified, the shadow will become a solid shape. The higher the value, the more the shadow.
Fuzzy. We will soon see examples.
*/
CGContextSetShadowWithColor (currentContext, CGSizeMake (10.0f, 10.0f), 20366f, [[UIColor grayColor] CGColor]);
/* Create the path first. Just the path handle .*/
CGMutablePathRef path = CGPathCreateMutable ();
CGRect firstRect = CGRectMake (55.0f, 60366f, 150366f, 150366f );
CGPathAddRect (path, NULL, firstRect );
CGContextAddPath (currentContext, path );
[[UIColor colorWithRed: 0.20f green: 0.60f blue: 0.80f alpha: 1.0f] setFill];
CGContextDrawPath (currentContext, kCGPathFill );
CGPathRelease (path );
}
-(Void) drawRectAtBottomOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
CGMutablePathRef secondPath = CGPathCreateMutable ();
CGRect secondRect = CGRectMake (1500000f, 2500000f, 1000000f, 1000000f );
CGPathAddRect (secondPath, NULL, secondRect );
CGContextAddPath (currentContext, secondPath );
[[UIColor purpleColor] setFill];
CGContextDrawPath (currentContext, kCGPathFill );
CGPathRelease (secondPath );
}

-(Void) drawRect :( CGRect) rect {
[Self drawRectAtTopOfScreen];
// Since the first rectangle has a shadow, the shadow still exists even if the second rectangle is not set.
[Self drawRectAtBottomOfScreen];
}

-(Void) drawRectAtTopOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
// Set the background color in gray
/* Second parameter: the shadow displacement, specified by the CGSize type value, starting from the right lower part of the shadow to be applied in each shape. The larger the x value of the displacement, the shape
The shadow on the right spreads farther and farther. The larger the y value of the displacement, the lower the shadow.
The third parameter is the shadow blur value, which is specified by the floating point value (CGFloat. If 0.0f is specified, the shadow will become a solid shape. The higher the value, the more the shadow.
Fuzzy. We will soon see examples.
*/
CGContextSetShadowWithColor (currentContext, CGSizeMake (10.0f, 10.0f), 20366f, [[UIColor grayColor] CGColor]);
/* Create the path first. Just the path handle .*/
CGMutablePathRef path = CGPathCreateMutable ();
CGRect firstRect = CGRectMake (55.0f, 60366f, 150366f, 150366f );
CGPathAddRect (path, NULL, firstRect );
CGContextAddPath (currentContext, path );
[[UIColor colorWithRed: 0.20f green: 0.60f blue: 0.80f alpha: 1.0f] setFill];
CGContextDrawPath (currentContext, kCGPathFill );
CGPathRelease (path );
}
-(Void) drawRectAtBottomOfScreen {
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
CGMutablePathRef secondPath = CGPathCreateMutable ();
CGRect secondRect = CGRectMake (1500000f, 2500000f, 1000000f, 1000000f );
CGPathAddRect (secondPath, NULL, secondRect );
CGContextAddPath (currentContext, secondPath );
[[UIColor purpleColor] setFill];
CGContextDrawPath (currentContext, kCGPathFill );
CGPathRelease (secondPath );
}
 

Related Article

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.