CoreGraphicsで葉っぱを書いて円形に並べてみた


![スクリーンショット 2014-04-10 23.53.28.png](https://qiita-image-store.s3.amazonaws.com/0/40693/d8a5e08d-1b48-bb7a-5099-d5789077f0b0.png "スクリーンショット 2014-04-10 23.53.28.png")

```Leaf.m


- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
    
    CGFloat center = rect.size.width/4;
    
    [self drawLeaf:context rect:rect center:center];
    
}

-(void)drawLeaf:(CGContextRef)context rect:(CGRect)rect center:(CGFloat)center{
    CGContextSaveGState(context);
    
    CGSize leafSize = CGSizeMake(130, 150);
    CGLayerRef leafLayer = CGLayerCreateWithContext(context, leafSize, NULL);
    CGContextRef leafContext = CGLayerGetContext(leafLayer);
    
    [self leaf:leafContext rect:rect center:center];
    [self line:leafContext rect:rect center:center];
    [self line2:leafContext rect:rect center:center x:55 y:75 slope:10];
    [self line2:leafContext rect:rect center:center x:50 y:95 slope:10];
    [self line2:leafContext rect:rect center:center x:60 y:115 slope:7];
    
    
    //描画
    CGContextTranslateCTM (context, 160, 200);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer); //実際の描画されてる図を考慮してpointmakeする
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer); //前の角度に足される
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);
    CGContextRotateCTM(context, 45.0 * M_PI/180);
    CGContextDrawLayerAtPoint(context, CGPointMake(-80, 10), leafLayer);

    CGLayerRelease(leafLayer);
    CGContextRestoreGState(context);
}

-(void)leaf:(CGContextRef)context rect:(CGRect)rect center:(CGFloat)center{
    CGContextSaveGState(context);
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    CGPoint startPoint = CGPointMake(40, 0);
    CGPoint endPoint = CGPointMake(40, 150);
    
    CGColorRef  startColor = [UIColor colorWithRed:34.0/255.0 green:139.0/255.0 blue:34.0/255.0 alpha:1.0].CGColor;
    CGColorRef  endColor = [UIColor colorWithRed:46.0/255.0 green:139.0/255.0 blue:87.0/255.0 alpha:1.0].CGColor;
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, center, 50);
    CGPathAddCurveToPoint(path, nil, 15, 70, 40, 120, 80, 150);
//    CGPathAddQuadCurveToPoint(path, nil, 160, 435, 180, 400);
    CGPathAddCurveToPoint(path, nil, 120, 120, 145, 70, 80, 50);
    
    CGPathCloseSubpath(path);
    
    //CGPathAddQuadCurveToPoint(path, nil, 50, 120, 160, 420);
    
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    //CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
    //CGContextStrokePath(context);
    CGContextFillPath(context);
    
    CGPathRelease(path);
    CGContextRestoreGState(context);
}

-(void)line:(CGContextRef)context rect:(CGRect)rect center:(CGFloat)center {
    CGContextSaveGState(context);
    
    UIColor *YellowGreen = [UIColor colorWithRed:154.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGContextMoveToPoint(context, center, 40);
    CGContextSetStrokeColorWithColor(context, YellowGreen.CGColor);
    CGContextSetLineWidth(context, 3);
    CGContextSetLineCap(context, kCGLineCapRound); // 先端を丸める
    CGContextAddQuadCurveToPoint(context, center, 80, center, 135);
    CGContextStrokePath(context);
    
    
    CGPathRelease(path);
}

-(void)line2:(CGContextRef)context rect:(CGRect)rect center:(CGFloat)center x:(CGFloat)x y:(CGFloat)y
       slope:(CGFloat)slope {
    CGContextSaveGState(context);
    
    UIColor *YellowGreen = [UIColor colorWithRed:154.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGContextMoveToPoint(context, x, y);
    CGContextSetStrokeColorWithColor(context, YellowGreen.CGColor);
    CGContextSetLineWidth(context, 3);
    CGContextSetLineCap(context, kCGLineCapRound); // 先端を丸める
    //    CGContextAddQuadCurveToPoint(context, 160, 80, 160, 400);
    CGContextAddLineToPoint(context, center, y-slope);
    CGContextAddLineToPoint(context, center + (center - x), y);
    CGContextStrokePath(context);
    
    
    CGPathRelease(path);
}


@end