1 前言
使用 CGContextSetShadow 過程,為繪制在圖形環境上的形狀應用陰影。
CGContextSetShadowWithColor 過程:這個過程接受的參數和 CGContextSetShadow 完全相同,不過加了一個 CGColorRef 類型的參數,用於設 置陰影的顏色。
2 代碼實例
ZYViewControllerView.m
-(void)drawRect:(CGRect)rect{
[self drawRectAtTopOfScreen];
//由於第一個矩形畫的時候已經有了陰影,所以就算第二個矩形繪圖時候沒有設置依然有陰影
[self drawRectAtBottomOfScreen];
}
- (void) drawRectAtTopOfScreen{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//用灰色設置背景顏色
/*第二個參數:陰影的位移,由 CGSize 類型值指定,從每個形狀要應用陰影的右下部分開始。位移的 x 值越大,形狀
右邊的陰影就擴散得越遠。位移的 y 值越大,下部的陰影就越低。
第三個參數:陰影的模糊值,以浮點值(CGFloat)來指定。指定 0.0f 將導致陰影成為固態形狀。這個值越高,陰影就越
模糊。我們很快能看到例子。
*/
CGContextSetShadowWithColor(currentContext,CGSizeMake(10.0f, 10.0f), 20.0f,[[UIColor grayColor] CGColor]);
/* Create the path first. Just the path handle. */
CGMutablePathRef path = CGPathCreateMutable();
CGRect firstRect = CGRectMake(55.0f, 60.0f,150.0f, 150.0f);
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(150.0f, 250.0f, 100.0f,100.0f);
CGPathAddRect(secondPath, NULL,secondRect);
CGContextAddPath(currentContext, secondPath);
[[UIColor purpleColor] setFill];
CGContextDrawPath(currentContext, kCGPathFill);
CGPathRelease(secondPath);
}
-(void)drawRect:(CGRect)rect{
[self drawRectAtTopOfScreen];
//由於第一個矩形畫的時候已經有了陰影,所以就算第二個矩形繪圖時候沒有設置依然有陰影
[self drawRectAtBottomOfScreen];
}
- (void) drawRectAtTopOfScreen{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//用灰色設置背景顏色
/*第二個參數:陰影的位移,由 CGSize 類型值指定,從每個形狀要應用陰影的右下部分開始。位移的 x 值越大,形狀
右邊的陰影就擴散得越遠。位移的 y 值越大,下部的陰影就越低。
第三個參數:陰影的模糊值,以浮點值(CGFloat)來指定。指定 0.0f 將導致陰影成為固態形狀。這個值越高,陰影就越
模糊。我們很快能看到例子。
*/
CGContextSetShadowWithColor(currentContext,CGSizeMake(10.0f, 10.0f), 20.0f,[[UIColor grayColor] CGColor]);
/* Create the path first. Just the path handle. */
CGMutablePathRef path = CGPathCreateMutable();
CGRect firstRect = CGRectMake(55.0f, 60.0f,150.0f, 150.0f);
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(150.0f, 250.0f, 100.0f,100.0f);
CGPathAddRect(secondPath, NULL,secondRect);
CGContextAddPath(currentContext, secondPath);
[[UIColor purpleColor] setFill];
CGContextDrawPath(currentContext, kCGPathFill);
CGPathRelease(secondPath);
}