forked from Kentzo/IKSegmentedControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IKSeparators.m
71 lines (61 loc) · 2.87 KB
/
IKSeparators.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
#import "IKSeparators.h"
#import "IKSegment.h"
#import "IKSegmentedControl.h"
@implementation IKSeparators
@synthesize segmentedControl;
@synthesize separator;
@synthesize separateSelectedItem;
- (void)setSeparator:(id)newSeparator {
separator = newSeparator;
[self setNeedsDisplay];
}
- (id)initWithSegmentedControl:(IKSegmentedControl *)aSegmentedControl {
if ((self = [super initWithFrame:aSegmentedControl.bounds])) {
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.userInteractionEnabled = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
separator = [UIColor lightGrayColor];
segmentedControl = aSegmentedControl;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, FALSE);
for (int i=1, end=[segmentedControl.segments count]-1; i<end; ++i) {
if (i == segmentedControl.selectedSegmentIndex && !separateSelectedItem) {
continue;
}
CGRect segmentFrame = [[segmentedControl.segments objectAtIndex:i] frame];
if ([separator isKindOfClass:[UIColor class]]) {
[separator setStroke];
CGContextSetLineWidth(context, 1.0f);
if (i != segmentedControl.selectedSegmentIndex + 1 || separateSelectedItem) {
CGContextBeginPath(context);
CGContextMoveToPoint(context, CGRectGetMinX(segmentFrame), CGRectGetMinY(segmentFrame));
CGContextAddLineToPoint(context, CGRectGetMinX(segmentFrame), CGRectGetMaxY(segmentFrame));
CGContextStrokePath(context);
}
if (i != segmentedControl.selectedSegmentIndex - 1 || separateSelectedItem) {
CGContextBeginPath(context);
CGContextMoveToPoint(context, CGRectGetMaxX(segmentFrame), CGRectGetMinY(segmentFrame));
CGContextAddLineToPoint(context, CGRectGetMaxX(segmentFrame), CGRectGetMaxY(segmentFrame));
CGContextStrokePath(context);
}
}
else {
if (i != segmentedControl.selectedSegmentIndex + 1 || separateSelectedItem) {
CGRect rect = CGRectMake(CGRectGetMinX(segmentFrame) - [separator size].width/2, CGRectGetMinY(segmentFrame),
[separator size].width, CGRectGetHeight(segmentFrame));
[separator drawInRect:rect];
}
if (i != segmentedControl.selectedSegmentIndex - 1 || separateSelectedItem) {
CGRect rect = CGRectMake(CGRectGetMaxX(segmentFrame) - [separator size].width/2, CGRectGetMinY(segmentFrame),
[separator size].width, CGRectGetHeight(segmentFrame));
[separator drawInRect:rect];
}
}
}
}
@end