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 );
}