| | 16 | |
| | 17 | |
| | 18 | |
| | 19 | {{{ |
| | 20 | |
| | 21 | #import "MATCAlertView.h" |
| | 22 | |
| | 23 | |
| | 24 | @implementation MATCAlertView |
| | 25 | |
| | 26 | #pragma mark - |
| | 27 | #pragma mark Initialization and teardown |
| | 28 | |
| | 29 | - (id)initWithFrame:(CGRect)frame { |
| | 30 | if (self = [super initWithFrame:frame]) { |
| | 31 | // Initialization code |
| | 32 | } |
| | 33 | return self; |
| | 34 | } |
| | 35 | |
| | 36 | |
| | 37 | - (void)dealloc { |
| | 38 | [super dealloc]; |
| | 39 | } |
| | 40 | |
| | 41 | #pragma mark - |
| | 42 | #pragma mark Drawing methods |
| | 43 | |
| | 44 | + (CGColorSpaceRef)genericRGBSpace; |
| | 45 | { |
| | 46 | static CGColorSpaceRef space = NULL; |
| | 47 | if (space == NULL) |
| | 48 | { |
| | 49 | space = CGColorSpaceCreateDeviceRGB(); |
| | 50 | } |
| | 51 | return space; |
| | 52 | } |
| | 53 | |
| | 54 | + (CGColorRef)whiteColor; |
| | 55 | { |
| | 56 | static CGColorRef white = NULL; |
| | 57 | if (white == NULL) |
| | 58 | { |
| | 59 | CGFloat values[4] = {1.0, 1.0, 1.0, 1.0}; |
| | 60 | white = CGColorCreate([self genericRGBSpace], values); |
| | 61 | } |
| | 62 | return white; |
| | 63 | } |
| | 64 | |
| | 65 | + (CGColorRef)shadowColor; |
| | 66 | { |
| | 67 | static CGColorRef shadowColor = NULL; |
| | 68 | if (shadowColor == NULL) |
| | 69 | { |
| | 70 | CGFloat values[4] = {0.0, 0.0, 0.0, 0.5}; |
| | 71 | shadowColor = CGColorCreate([self genericRGBSpace], values); |
| | 72 | } |
| | 73 | return shadowColor; |
| | 74 | } |
| | 75 | |
| | 76 | |
| | 77 | + (CGColorRef)translucentBlueColor; |
| | 78 | { |
| | 79 | static CGColorRef translucentBlue = NULL; |
| | 80 | if (translucentBlue == NULL) |
| | 81 | { |
| | 82 | CGFloat values[4] = {0.13, 0.24, 0.44, 0.7}; |
| | 83 | translucentBlue = CGColorCreate([self genericRGBSpace], values); |
| | 84 | } |
| | 85 | return translucentBlue; |
| | 86 | } |
| | 87 | |
| | 88 | - (void)drawRect:(CGRect)rect |
| | 89 | { |
| | 90 | CGFloat shadowRadius = 4.0f; |
| | 91 | CGContextRef context = UIGraphicsGetCurrentContext(); |
| | 92 | CGRect currentBounds = self.bounds; |
| | 93 | |
| | 94 | CGContextSaveGState(context); |
| | 95 | // Set up the shadow around the view, which requires a slight inset on the view |
| | 96 | CGContextSetShadowWithColor( context, CGSizeMake(0.0f, -shadowRadius), shadowRadius, [MATCAlertView shadowColor] ); |
| | 97 | |
| | 98 | // Draw the blue background rectangle and border |
| | 99 | CGContextSetLineWidth(context, 2.0f); |
| | 100 | CGContextSetStrokeColorWithColor(context, [MATCAlertView whiteColor]); |
| | 101 | CGContextSetFillColorWithColor(context, [MATCAlertView translucentBlueColor]); |
| | 102 | |
| | 103 | CGFloat cornerRadius = 5.0f; |
| | 104 | CGRect actualViewRect = CGRectMake(shadowRadius + 0.5, 1.5f, currentBounds.size.width - shadowRadius * 2.0f, currentBounds.size.height - shadowRadius*2.5f); |
| | 105 | |
| | 106 | CGContextBeginPath(context); |
| | 107 | CGContextMoveToPoint(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + 0.5); |
| | 108 | CGContextAddArc(context, CGRectGetMaxX(actualViewRect) - cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + cornerRadius + 0.5, cornerRadius, 3 * M_PI / 2, 0, 0); |
| | 109 | CGContextAddArc(context, CGRectGetMaxX(actualViewRect) - cornerRadius + 0.5, CGRectGetMaxY(actualViewRect) - cornerRadius + 0.5, cornerRadius, 0, M_PI / 2, 0); |
| | 110 | CGContextAddArc(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMaxY(actualViewRect) - cornerRadius + 0.5, cornerRadius, M_PI / 2, M_PI, 0); |
| | 111 | CGContextAddArc(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + cornerRadius + 0.5, cornerRadius, M_PI, 3 * M_PI / 2, 0); |
| | 112 | CGContextClosePath(context); |
| | 113 | CGContextDrawPath(context, kCGPathFillStroke); |
| | 114 | CGContextRestoreGState(context); |
| | 115 | // |
| | 116 | // // Clip the gloss clipping path to the rounded rectangle |
| | 117 | // CGContextBeginPath(context); |
| | 118 | // CGContextMoveToPoint(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + 0.5); |
| | 119 | // CGContextAddArc(context, CGRectGetMaxX(actualViewRect) - cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + cornerRadius + 0.5, cornerRadius, 3 * M_PI / 2, 0, 0); |
| | 120 | // CGContextAddArc(context, CGRectGetMaxX(actualViewRect) - cornerRadius + 0.5, CGRectGetMaxY(actualViewRect) - cornerRadius + 0.5, cornerRadius, 0, M_PI / 2, 0); |
| | 121 | // CGContextAddArc(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMaxY(actualViewRect) - cornerRadius + 0.5, cornerRadius, M_PI / 2, M_PI, 0); |
| | 122 | // CGContextAddArc(context, CGRectGetMinX(actualViewRect) + cornerRadius + 0.5, CGRectGetMinY(actualViewRect) + cornerRadius + 0.5, cornerRadius, M_PI, 3 * M_PI / 2, 0); |
| | 123 | // CGContextClosePath(context); |
| | 124 | // CGContextClip(context); |
| | 125 | // |
| | 126 | // // Set up a clipping path for the gloss gradient |
| | 127 | // CGFloat glossRadius = 600.0f; |
| | 128 | // CGPoint glossCenterPoint = CGPointMake(CGRectGetMidX(currentBounds), 30.0 - glossRadius); |
| | 129 | // CGContextBeginPath(context); |
| | 130 | // CGContextMoveToPoint(context, glossCenterPoint.x, glossCenterPoint.y); |
| | 131 | // CGContextAddArc(context, glossCenterPoint.x, glossCenterPoint.y, glossRadius, 0.0, M_PI, 0 |
| | 132 | // ); |
| | 133 | // CGContextClosePath(context); |
| | 134 | // CGContextClip(context); |
| | 135 | // |
| | 136 | // // Draw the gloss gradient |
| | 137 | CGGradientRef glossGradient; |
| | 138 | CGFloat locations[2] = { 0.0, 1.0 }; |
| | 139 | CGFloat components[8] = { 1.0, 1.0, 1.0, 0.65, // Start color |
| | 140 | 1.0, 1.0, 1.0, 0.06 }; // End color |
| | 141 | glossGradient = CGGradientCreateWithColorComponents([MATCAlertView genericRGBSpace], components, locations, 2); |
| | 142 | CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); |
| | 143 | CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), 30.0f); |
| | 144 | CGContextDrawLinearGradient(context, glossGradient, topCenter, midCenter, 0); |
| | 145 | CGGradientRelease(glossGradient); |
| | 146 | } |
| | 147 | |
| | 148 | |
| | 149 | |
| | 150 | |
| | 151 | @end |
| | 152 | |
| | 153 | }}} |
| | 154 | |