|
| 1 | +// |
| 2 | +// UACellBackgroundView.m |
| 3 | +// FlatUI |
| 4 | +// |
| 5 | +// Created by Maciej Swic on 2013-05-30. |
| 6 | +// Licensed under the MIT license. |
| 7 | + |
| 8 | +#import "FUICellBackgroundView.h" |
| 9 | + |
| 10 | +@implementation FUICellBackgroundView |
| 11 | + |
| 12 | +- (id)initWithFrame:(CGRect)frame { |
| 13 | + if ((self = [super initWithFrame:frame])) { |
| 14 | + self.cornerRadius = 10.f; |
| 15 | + self.separatorHeight = 1.f; |
| 16 | + } |
| 17 | + |
| 18 | + return self; |
| 19 | +} |
| 20 | + |
| 21 | +- (id)initWithFrame:(CGRect)frame andTableView:(UITableView*)tableView andIndexPath:(NSIndexPath*)indexPath { |
| 22 | + if ((self = [self initWithFrame:frame])) { |
| 23 | + if ([tableView numberOfRowsInSection:indexPath.section] == 1) |
| 24 | + self.position = FUICellBackgroundViewPositionSingle; |
| 25 | + else if (indexPath.row == 0) |
| 26 | + self.position = FUICellBackgroundViewPositionTop; |
| 27 | + else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) |
| 28 | + self.position = FUICellBackgroundViewPositionBottom; |
| 29 | + else |
| 30 | + self.position = UACellBackgroundViewPositionMiddle; |
| 31 | + |
| 32 | + self.separatorColor = tableView.separatorColor; |
| 33 | + } |
| 34 | + |
| 35 | + return self; |
| 36 | +} |
| 37 | + |
| 38 | +- (BOOL)isOpaque { |
| 39 | + return NO; |
| 40 | +} |
| 41 | + |
| 42 | +-(void)drawRect:(CGRect)aRect { |
| 43 | + CGContextRef c = UIGraphicsGetCurrentContext(); |
| 44 | + |
| 45 | + int lineWidth = 1; |
| 46 | + |
| 47 | + CGRect rect = [self bounds]; |
| 48 | + CGFloat minX = CGRectGetMinX(rect), midX = CGRectGetMidX(rect), maxX = CGRectGetMaxX(rect); |
| 49 | + CGFloat minY = CGRectGetMinY(rect), midY = CGRectGetMidY(rect), maxY = CGRectGetMaxY(rect); |
| 50 | + minY -= 1; |
| 51 | + |
| 52 | + CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); |
| 53 | + CGContextSetStrokeColorWithColor(c, [[UIColor grayColor] CGColor]); |
| 54 | + CGContextSetLineWidth(c, lineWidth); |
| 55 | + CGContextSetAllowsAntialiasing(c, YES); |
| 56 | + CGContextSetShouldAntialias(c, YES); |
| 57 | + |
| 58 | + if (self.position == FUICellBackgroundViewPositionTop) { |
| 59 | + minY += 1; |
| 60 | + |
| 61 | + CGMutablePathRef path = CGPathCreateMutable(); |
| 62 | + CGPathMoveToPoint(path, NULL, minX, maxY); |
| 63 | + CGPathAddArcToPoint(path, NULL, minX, minY, midX, minY, self.cornerRadius); |
| 64 | + CGPathAddArcToPoint(path, NULL, maxX, minY, maxX, maxY, self.cornerRadius); |
| 65 | + CGPathAddLineToPoint(path, NULL, maxX, maxY); |
| 66 | + CGPathAddLineToPoint(path, NULL, minX, maxY); |
| 67 | + CGPathCloseSubpath(path); |
| 68 | + |
| 69 | + CGContextSaveGState(c); |
| 70 | + CGContextAddPath(c, path); |
| 71 | + CGContextClip(c); |
| 72 | + |
| 73 | + CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor); |
| 74 | + CGContextFillRect(c, self.bounds); |
| 75 | + CGContextSetFillColorWithColor(c, self.separatorColor.CGColor); |
| 76 | + CGContextFillRect(c, CGRectMake(0, self.bounds.size.height - self.separatorHeight, self.bounds.size.width, self.bounds.size.height - self.separatorHeight)); |
| 77 | + |
| 78 | + CGContextAddPath(c, path); |
| 79 | + CGPathRelease(path); |
| 80 | + CGContextRestoreGState(c); |
| 81 | + } else if (self.position == FUICellBackgroundViewPositionBottom) { |
| 82 | + CGMutablePathRef path = CGPathCreateMutable(); |
| 83 | + CGPathMoveToPoint(path, NULL, minX, minY); |
| 84 | + CGPathAddArcToPoint(path, NULL, minX, maxY, midX, maxY, self.cornerRadius); |
| 85 | + CGPathAddArcToPoint(path, NULL, maxX, maxY, maxX, minY, self.cornerRadius); |
| 86 | + CGPathAddLineToPoint(path, NULL, maxX, minY); |
| 87 | + CGPathAddLineToPoint(path, NULL, minX, minY); |
| 88 | + CGPathCloseSubpath(path); |
| 89 | + |
| 90 | + CGContextSaveGState(c); |
| 91 | + CGContextAddPath(c, path); |
| 92 | + CGContextClip(c); |
| 93 | + |
| 94 | + CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor); |
| 95 | + CGContextFillRect(c, self.bounds); |
| 96 | + |
| 97 | + CGContextAddPath(c, path); |
| 98 | + CGPathRelease(path); |
| 99 | + CGContextRestoreGState(c); |
| 100 | + } else if (self.position == UACellBackgroundViewPositionMiddle) { |
| 101 | + CGMutablePathRef path = CGPathCreateMutable(); |
| 102 | + CGPathMoveToPoint(path, NULL, minX, minY); |
| 103 | + CGPathAddLineToPoint(path, NULL, maxX, minY); |
| 104 | + CGPathAddLineToPoint(path, NULL, maxX, maxY); |
| 105 | + CGPathAddLineToPoint(path, NULL, minX, maxY); |
| 106 | + CGPathAddLineToPoint(path, NULL, minX, minY); |
| 107 | + CGPathCloseSubpath(path); |
| 108 | + |
| 109 | + CGContextSaveGState(c); |
| 110 | + CGContextAddPath(c, path); |
| 111 | + CGContextClip(c); |
| 112 | + |
| 113 | + CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor); |
| 114 | + CGContextFillRect(c, self.bounds); |
| 115 | + CGContextSetFillColorWithColor(c, self.separatorColor.CGColor); |
| 116 | + CGContextFillRect(c, CGRectMake(0, self.bounds.size.height - self.separatorHeight, self.bounds.size.width, self.bounds.size.height - self.separatorHeight)); |
| 117 | + |
| 118 | + CGContextAddPath(c, path); |
| 119 | + CGPathRelease(path); |
| 120 | + CGContextRestoreGState(c); |
| 121 | + } else if (self.position == FUICellBackgroundViewPositionSingle) { |
| 122 | + minY += 1; |
| 123 | + |
| 124 | + CGMutablePathRef path = CGPathCreateMutable(); |
| 125 | + CGPathMoveToPoint(path, NULL, minX, midY); |
| 126 | + CGPathAddArcToPoint(path, NULL, minX, minY, midX, minY, self.cornerRadius); |
| 127 | + CGPathAddArcToPoint(path, NULL, maxX, minY, maxX, midY, self.cornerRadius); |
| 128 | + CGPathAddArcToPoint(path, NULL, maxX, maxY, midX, maxY, self.cornerRadius); |
| 129 | + CGPathAddArcToPoint(path, NULL, minX, maxY, minX, midY, self.cornerRadius); |
| 130 | + CGPathCloseSubpath(path); |
| 131 | + |
| 132 | + CGContextSaveGState(c); |
| 133 | + CGContextAddPath(c, path); |
| 134 | + CGContextClip(c); |
| 135 | + |
| 136 | + CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor); |
| 137 | + CGContextFillRect(c, self.bounds); |
| 138 | + |
| 139 | + CGContextAddPath(c, path); |
| 140 | + CGPathRelease(path); |
| 141 | + CGContextRestoreGState(c); |
| 142 | + } |
| 143 | + |
| 144 | + CGColorSpaceRelease(colorspace); |
| 145 | +} |
| 146 | + |
| 147 | +- (void)setPosition:(FUICellBackgroundViewPosition)position { |
| 148 | + _position = position; |
| 149 | + |
| 150 | + [self setNeedsDisplay]; |
| 151 | +} |
| 152 | + |
| 153 | +@end |
0 commit comments