2012-08-15 150 views
0

下面是我想绘制在核心图形中的一面旗帜。如何添加阴影,核心图形

enter image description here

我要做的就是:

Flag.m 
- (void)drawRect:(CGRect)rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 1.0); 

    CGContextMoveToPoint (context, 20, 10); 
    CGContextAddLineToPoint (context, 50, 10); 
    CGContextAddLineToPoint (context, 50, 90); 
    CGContextAddLineToPoint (context, 45, 90); 
    CGContextAddLineToPoint (context, 45, 95); 
    CGContextAddLineToPoint (context, 40, 92); 
    CGContextAddLineToPoint (context, 35, 90); 
    CGContextAddLineToPoint (context, 30, 92); 
    CGContextAddLineToPoint (context, 25, 95); 
    CGContextAddLineToPoint (context, 25, 90); 
    CGContextAddLineToPoint (context, 20, 90); 
    CGContextAddLineToPoint (context, 20, 10);  

    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextFillPath(context); 

} 

什么我结束了下面是

enter image description here

我的问题:

如何添加阴影所以我的国旗似乎是折叠d并在其尾部卷曲(如上面的红旗)

回答

1

这是一个相对简化的版本。我评论了几个要点,将其分为两部分。尾部首先被绘制。然后,主要功能区在上方绘制。一个不错的阴影效果可以用一个[UIColor colorWithWhite:0.0 alpha:0.4]

如果你想更有趣,你可以绘制主区域底部的几个点作为另一个具有梯度的矩形。确保在绘制最后一个矩形之前关闭阴影。

/*CGContextMoveToPoint (context, 20, 10); 
CGContextAddLineToPoint (context, 50, 10);*/ 
CGContextMoveToPoint (context, 48, 85); 
CGContextAddLineToPoint (context, 48, 90); 
CGContextAddLineToPoint (context, 45, 90); 
CGContextAddLineToPoint (context, 45, 95); 
CGContextAddLineToPoint (context, 40, 92); 
CGContextAddLineToPoint (context, 35, 90); 
CGContextAddLineToPoint (context, 30, 92); 
CGContextAddLineToPoint (context, 25, 95); 
CGContextAddLineToPoint (context, 25, 90); 
CGContextAddLineToPoint (context, 22, 90); 
CGContextAddLineToPoint (context, 22, 85); 
//CGContextAddLineToPoint (context, 20, 10); 
CGContextSetShadowWithColor(context, CGSizeMake(0, 5.), 3., [UIColor colorWithWhite:0 alpha:.4].CGColor); 
CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:.95 alpha:1].CGColor); 
CGContextFillPath(context); 
CGContextSetShadowWithColor(context, CGSizeMake(0, 2.), 1.5, [UIColor colorWithWhite:0 alpha:.3].CGColor); 
CGContextMoveToPoint (context, 20, 88); 
CGContextAddLineToPoint (context, 20, 10); 
CGContextAddLineToPoint (context, 50, 10); 
CGContextAddLineToPoint (context, 50, 88); 
CGContextFillPath(context);