-
Notifications
You must be signed in to change notification settings - Fork 9
/
RoundedUITableView.m
125 lines (87 loc) · 2.74 KB
/
RoundedUITableView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// RoundedUITableView.m
// RoundedTableView
//
// Created by Jeremy Collins on 1/7/10.
// Copyright 2010 Beetlebug Software, LLC. All rights reserved.
//
#import "RoundedUITableView.h"
#import <QuartzCore/QuartzCore.h>
@implementation RoundedUITableViewMask
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = NO;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat cornerRadius = 10.0;
CGFloat minx = CGRectGetMinX(rect);
CGFloat midx = CGRectGetMidX(rect);
CGFloat maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect);
CGFloat midy = CGRectGetMidY(rect);
CGFloat maxy = CGRectGetMaxY(rect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, cornerRadius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, cornerRadius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, cornerRadius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, cornerRadius);
CGContextClosePath(context);
CGContextFillPath(context);
}
- (void)dealloc {
[super dealloc];
}
@end
@implementation RoundedUITableView
- (void)setupView {
mask = [[RoundedUITableViewMask alloc] initWithFrame:CGRectZero];
self.layer.mask = mask.layer;
self.layer.cornerRadius = 10;
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self setupView];
}
return self;
}
- (void)adjustMask {
// Re-calculate the size of the mask to account for adding/removing rows.
CGRect frame = mask.frame;
if(self.contentSize.height > self.frame.size.height) {
frame.size = self.contentSize;
} else {
frame.size = self.frame.size;
}
mask.frame = frame;
[mask setNeedsDisplay];
[self setNeedsDisplay];
}
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self adjustMask];
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
[super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
[self adjustMask];
}
- (void)reloadData {
[super reloadData];
[self adjustMask];
}
- (void)dealloc {
[mask release];
[super dealloc];
}
@end