Skip to content

Instantly share code, notes, and snippets.

@tomohisa
Created May 12, 2012 08:59
Show Gist options
  • Save tomohisa/2665336 to your computer and use it in GitHub Desktop.
Save tomohisa/2665336 to your computer and use it in GitHub Desktop.
paint code variable size sample
- (void)drawRect:(CGRect)rect
{
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* color = [UIColor colorWithRed: 0.3 green: 0.3 blue: 0.3 alpha: 1];
UIColor* color2 = [UIColor colorWithRed: 0.1 green: 0.1 blue: 0.1 alpha: 1];
UIColor* color3 = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.69];
//// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)color.CGColor,
(id)color2.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
CGColorRef shadow2 = color3.CGColor;
CGSize shadow2Offset = CGSizeMake(0, -0);
CGFloat shadow2BlurRadius = 20;
//// Abstracted Graphic Attributes
CGRect rectangleFrame = rect;
//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleFrame];
CGContextSaveGState(context);
[rectanglePath addClip];
CGContextDrawRadialGradient(context, gradient,
CGPointMake(rect.size.width/2.0, rect.size.height/2.0), 10,
CGPointMake(rect.size.width/2.0, rect.size.height/2.0), 680.98,
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
////// Rectangle Inner Shadow
CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -shadow2BlurRadius, -shadow2BlurRadius);
rectangleBorderRect = CGRectOffset(rectangleBorderRect, -shadow2Offset.width, -shadow2Offset.height);
rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);
UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
[rectangleNegativePath appendPath: rectanglePath];
rectangleNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(context);
{
CGFloat xOffset = shadow2Offset.width + round(rectangleBorderRect.size.width);
CGFloat yOffset = shadow2Offset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
shadow2BlurRadius,
shadow2);
[rectanglePath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
[rectangleNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[rectangleNegativePath fill];
}
CGContextRestoreGState(context);
UIImage *noise = [UIImage imageNamed:@"noise.png"];
CGSize imageViewSize = rect.size;
UIGraphicsBeginImageContext(imageViewSize);
CGRect imageRect;
imageRect.origin = CGPointMake(0.0, 0.0);
imageRect.size = CGSizeMake(noise.size.width, noise.size.height);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextSetAlpha(imageContext, 0.3);
CGContextDrawTiledImage(imageContext, imageRect, noise.CGImage);
UIImage *finishedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef cgImage = finishedImage.CGImage;
CGContextDrawImage(context, rect, cgImage);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment