Advertisement
Guest User

PaintCode's custom button drawRect

a guest
Jun 11th, 2013
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)drawRect:(CGRect)rect
  2. {
  3.     //// General Declarations
  4.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  5.     CGContextRef context = UIGraphicsGetCurrentContext();
  6.    
  7.     //// Color Declarations
  8.     UIColor* fillColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
  9.     UIColor* color = [UIColor colorWithRed: 0.919 green: 0.919 blue: 0.919 alpha: 1];
  10.     CGFloat colorRGBA[4];
  11.     [color getRed: &colorRGBA[0] green: &colorRGBA[1] blue: &colorRGBA[2] alpha: &colorRGBA[3]];
  12.    
  13.     UIColor* strokeColor = [UIColor colorWithRed: (colorRGBA[0] * 0.5 + 0.5) green: (colorRGBA[1] * 0.5 + 0.5) blue: (colorRGBA[2] * 0.5 + 0.5) alpha: (colorRGBA[3] * 0.5 + 0.5)];
  14.     UIColor* playerSelected = [UIColor colorWithRed: 1 green: 0.41 blue: 0.114 alpha: 1];
  15.    
  16.     //// Gradient Declarations
  17.     NSArray* gradientColors = [NSArray arrayWithObjects:
  18.                                (id)color.CGColor,
  19.                                (id)[UIColor colorWithRed: 0.959 green: 0.959 blue: 0.959 alpha: 1].CGColor,
  20.                                (id)fillColor.CGColor, nil];
  21.     CGFloat gradientLocations[] = {0, 0.08, 1};
  22.     CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
  23.    
  24.     //// Subframes
  25.     CGRect group = self.bounds;
  26.    
  27.    
  28.     //// Abstracted Attributes
  29.     CGRect rectangleRect = CGRectMake(CGRectGetMinX(group) + floor(CGRectGetWidth(group) * 0.00595) + 0.5, CGRectGetMinY(group) + floor(CGRectGetHeight(group) * 0.00862) + 0.5, floor(CGRectGetWidth(group) * 0.99405) - floor(CGRectGetWidth(group) * 0.00595), floor(CGRectGetHeight(group) * 0.99138) - floor(CGRectGetHeight(group) * 0.00862));
  30.    
  31.    
  32.     //// Group
  33.     {
  34.         //// Rectangle Drawing
  35.         UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: rectangleRect];
  36.         CGContextSaveGState(context);
  37.         [rectanglePath addClip];
  38.         CGContextDrawLinearGradient(context, gradient,
  39.                                     CGPointMake(CGRectGetMidX(rectangleRect) + 6.28 * CGRectGetWidth(rectangleRect) / 83, CGRectGetMidY(rectangleRect) + -4.95 * CGRectGetHeight(rectangleRect) / 57),
  40.                                     CGPointMake(CGRectGetMidX(rectangleRect) + 8.86 * CGRectGetWidth(rectangleRect) / 83, CGRectGetMidY(rectangleRect) + 78.16 * CGRectGetHeight(rectangleRect) / 57),
  41.                                     kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
  42.         CGContextRestoreGState(context);
  43.         [strokeColor setStroke];
  44.         rectanglePath.lineWidth = 0.5;
  45.         CGFloat rectanglePattern[] = {1, 1, 1, 1};
  46.         [rectanglePath setLineDash: rectanglePattern count: 4 phase: 0];
  47.         [rectanglePath stroke];
  48.        
  49.        
  50.         //// Icon Drawing
  51.         UIBezierPath* iconPath = [UIBezierPath bezierPath];
  52.         [iconPath moveToPoint: CGPointMake(CGRectGetMinX(group) + 0.54481 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68629 * CGRectGetHeight(group))];
  53.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.31744 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68629 * CGRectGetHeight(group))];
  54.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.36583 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.59828 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.31744 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.63718 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.31340 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61012 * CGRectGetHeight(group))];
  55.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.44345 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.53242 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.41715 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.58669 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.46780 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.57633 * CGRectGetHeight(group))];
  56.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.50037 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.32852 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.37129 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.40229 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.42288 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.32852 * CGRectGetHeight(group))];
  57.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.55729 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.53242 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.57637 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.32852 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.63512 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.40191 * CGRectGetHeight(group))];
  58.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.51816 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.62052 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.53839 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.56412 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.51816 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.59137 * CGRectGetHeight(group))];
  59.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.54481 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68629 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.51816 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.64594 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.52830 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.66908 * CGRectGetHeight(group))];
  60.         [iconPath closePath];
  61.         [iconPath moveToPoint: CGPointMake(CGRectGetMinX(group) + 0.68338 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.62052 * CGRectGetHeight(group))];
  62.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.61632 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68618 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.68338 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.65679 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.65335 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68618 * CGRectGetHeight(group))];
  63.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.54926 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.62052 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.57928 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.68618 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.54926 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.65679 * CGRectGetHeight(group))];
  64.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.61632 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.55486 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.54926 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.58426 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.57928 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.55486 * CGRectGetHeight(group))];
  65.         [iconPath addCurveToPoint: CGPointMake(CGRectGetMinX(group) + 0.68338 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.62052 * CGRectGetHeight(group)) controlPoint1: CGPointMake(CGRectGetMinX(group) + 0.65335 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.55486 * CGRectGetHeight(group)) controlPoint2: CGPointMake(CGRectGetMinX(group) + 0.68338 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.58426 * CGRectGetHeight(group))];
  66.         [iconPath closePath];
  67.         [iconPath moveToPoint: CGPointMake(CGRectGetMinX(group) + 0.65753 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61050 * CGRectGetHeight(group))];
  68.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.62850 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61050 * CGRectGetHeight(group))];
  69.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.62850 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.58208 * CGRectGetHeight(group))];
  70.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.60608 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.58208 * CGRectGetHeight(group))];
  71.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.60608 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61050 * CGRectGetHeight(group))];
  72.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.57706 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61050 * CGRectGetHeight(group))];
  73.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.57706 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.63245 * CGRectGetHeight(group))];
  74.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.60608 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.63245 * CGRectGetHeight(group))];
  75.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.60608 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.66087 * CGRectGetHeight(group))];
  76.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.62850 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.66087 * CGRectGetHeight(group))];
  77.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.62850 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.63245 * CGRectGetHeight(group))];
  78.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.65753 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.63245 * CGRectGetHeight(group))];
  79.         [iconPath addLineToPoint: CGPointMake(CGRectGetMinX(group) + 0.65753 * CGRectGetWidth(group), CGRectGetMinY(group) + 0.61050 * CGRectGetHeight(group))];
  80.         [iconPath closePath];
  81.         iconPath.miterLimit = 4;
  82.        
  83.         [playerSelected setFill];
  84.         [iconPath fill];
  85.     }
  86.    
  87.    
  88.     //// Cleanup
  89.     CGGradientRelease(gradient);
  90.     CGColorSpaceRelease(colorSpace);
  91.    
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement