This repository was archived by the owner on Mar 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathFSVerticalTabBarButton.m
More file actions
130 lines (101 loc) · 4.19 KB
/
Copy pathFSVerticalTabBarButton.m
File metadata and controls
130 lines (101 loc) · 4.19 KB
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
126
127
128
129
130
//
// FSVerticalTabBarButton.m
// iOS-Platform
//
// Created by Błażej Biesiada on 4/8/12.
// Copyright (c) 2012 Future Simple. All rights reserved.
//
#import "FSVerticalTabBarButton.h"
@implementation FSVerticalTabBarButton
@synthesize selectedImageTintColor = _selectedImageTintColor;
@synthesize iconImage = _iconImage;
- (UIColor *)selectedImageTintColor
{
// use blue tint by default
return _selectedImageTintColor != nil ? _selectedImageTintColor : [UIColor colorWithRed:41.0/255.0 green:147.0/255.0 blue:239.0/255.0 alpha:1.0];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textColor = [UIColor lightGrayColor];
self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect bounds = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
// flip the coordinates system
CGContextTranslateCTM(context, 0.0, bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw an image in the center of the cell
CGSize imageSize = self.iconImage.size;
CGRect imageRect = CGRectMake(floorf(((bounds.size.width-imageSize.width)/2.0)),
floorf(((bounds.size.height-imageSize.height)/2.0)+15),
imageSize.width,
imageSize.height);
// draw either a selection gradient/glow or a regular image
if (self.isSelected)
{
// setup shadow
CGSize shadowOffset = CGSizeMake(0.0f, 1.0f);
CGFloat shadowBlur = 3.0;
CGColorRef cgShadowColor = [[UIColor blackColor] CGColor];
// setup gradient
CGFloat alpha0 = 0.8;
CGFloat alpha1 = 0.6;
CGFloat alpha2 = 0.0;
CGFloat alpha3 = 0.1;
CGFloat alpha4 = 0.5;
CGFloat locations[5] = {0,0.55,0.55,0.7,1};
CGFloat components[20] = {1,1,1,alpha0,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3,1,1,1,alpha4};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)5);
CGColorSpaceRelease(colorSpace);
// set shadow
CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, cgShadowColor);
// set transparency layer and clip to mask
CGContextBeginTransparencyLayer(context, NULL);
CGContextClipToMask(context, imageRect, [self.iconImage CGImage]);
// fill and end the transparency layer
CGContextSetFillColorWithColor(context, [self.selectedImageTintColor CGColor]);
CGContextFillRect(context, imageRect);
CGPoint start = CGPointMake(CGRectGetMidX(imageRect), imageRect.origin.y);
CGPoint end = CGPointMake(CGRectGetMidX(imageRect)-imageRect.size.height/4, imageRect.size.height+imageRect.origin.y);
CGContextDrawLinearGradient(context, colorGradient, end, start, 0);
CGContextEndTransparencyLayer(context);
CGGradientRelease(colorGradient);
}
else
{
CGContextDrawImage(context,
imageRect,
self.iconImage.CGImage);
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect textLabelFrame = CGRectMake(0,
NAN,
self.bounds.size.width,
self.textLabel.font.lineHeight);
textLabelFrame.origin.y = self.bounds.size.height-textLabelFrame.size.height - 15;
self.textLabel.frame = textLabelFrame;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self setNeedsDisplay];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
[self setNeedsDisplay];
}
@end