Drop shadow on the triangle with gradient?


#1

How would you create a drop shadow for the triangle in addition to the image? He is my code for the gold and bronze challenge.

- (void)drawRect:(CGRect)rect
{
    
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    
    CGRect bounds = self.bounds;
    
    // Figure out the center of the bounds rectangle
    CGPoint center;
    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    
   // The largest circle will circumscribe the view
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
    
    UIBezierPath *path = [[UIBezierPath alloc] init];
    
    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
        
        [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
        
        [path addArcWithCenter:center
                        radius:currentRadius  // Note this is the current radius
                    startAngle:0.0
                      endAngle:M_PI * 2.0
                     clockwise:YES];
    }
    
    
    // Configure the line wdth to 10 points
    path.lineWidth = 10;
    
    [[UIColor lightGrayColor] setStroke];
    // Draw the line!
    [path stroke];
    
    
    
    
    // Drawing triangle
    UIBezierPath *trianglePath = [[UIBezierPath alloc] init];
    CGPoint triangleOne = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect) + 100);
    CGPoint triangleTwo = CGPointMake(CGRectGetMaxX(rect) - 50, CGRectGetMaxY(rect) - 100);
    CGPoint triangleThree = CGPointMake(CGRectGetMinX(rect) + 50, CGRectGetMaxY(rect) - 100);
    [trianglePath moveToPoint:triangleOne];
    [trianglePath addLineToPoint:triangleTwo];
    [trianglePath addLineToPoint:triangleThree];
    
    [trianglePath addClip];
    
    // gradient
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 2.0, 0.0, 4.0, 5.0, 1.0, 1.0, 0.0, 1.0};
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 2);
    CGPoint startpoint = CGPointMake(0, 0);
    CGPoint endpoint = CGPointMake(bounds.size.width, bounds.size.height);

 
    CGContextDrawLinearGradient(currentContext, gradient, startpoint, endpoint, 0);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    
    
    //important to place the restore in front of the 'logo code'
    // otherwise it will be clipped as well
    CGContextRestoreGState(currentContext);
    
    
    CGContextSetShadow(currentContext, CGSizeMake(7, 10), 3);


    UIImage *logoImage = [UIImage imageNamed:@"dice.png"];
    
    CGRect imageFrame = CGRectMake(center.x - (logoImage.size.width / 2),
                                   center.y - (logoImage.size.height / 2), logoImage.size.width, logoImage.size.height);
    [logoImage drawInRect:imageFrame];
    
}

I thought if I added [quote] CGContextSetShadow(currentContext, CGSizeMake(7, 10), 3); [/quote] before [quote] CGContextDrawLinearGradient(currentContext, gradient, startpoint, endpoint, 0); [/quote]
in the code above, it would draw the shadow for the gradient colored triangle, but that doesn’t work.