forked from michaelvillar/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MVBuddyViewCell.m
186 lines (159 loc) · 5.86 KB
/
MVBuddyViewCell.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#import "MVBuddyViewCell.h"
#import "MVGraphicsFunctions.h"
#import "PocketSVG.h"
#import "TUIView+Easing.h"
@interface MVBuddyViewCell ()
@property (strong, readwrite) TUIView *avatarView;
@property (strong, readwrite) TUIView *labelView;
@property (strong, readwrite) TUIView *arrowView;
@property (strong, readwrite) TUIView *drawView;
@end
@implementation MVBuddyViewCell
@synthesize email = email_,
fullname = fullname_,
online = online_,
avatar = avatar_,
alternate = alternate_,
firstRow = firstRow_,
lastRow = lastRow_,
representedObject = representedObject_;
@synthesize avatarView = avatarView_,
labelView = labelView_,
arrowView = arrowView_,
drawView = drawView_;
- (id)initWithStyle:(TUITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
email_ = @"";
fullname_ = @"";
online_ = NO;
avatar_ = nil;
alternate_ = NO;
firstRow_ = NO;
lastRow_ = NO;
representedObject_ = nil;
self.opaque = NO;
self.backgroundColor = [TUIColor clearColor];
self.clipsToBounds = NO;
__weak __block MVBuddyViewCell *weakSelf = self;
avatarView_ = [[TUIView alloc] initWithFrame:CGRectMake(7.5, 4, 22, 22)];
avatarView_.backgroundColor = [TUIColor clearColor];
avatarView_.userInteractionEnabled = NO;
avatarView_.drawRect = ^(TUIView *view, CGRect rect)
{
[[NSGraphicsContext currentContext] saveGraphicsState];
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:view.bounds];
[path addClip];
if(weakSelf.avatar)
[weakSelf.avatar drawInRect:view.bounds];
[[NSGraphicsContext currentContext] restoreGraphicsState];
};
labelView_ = [[TUIView alloc] initWithFrame:CGRectZero];
labelView_.backgroundColor = [TUIColor whiteColor];
labelView_.userInteractionEnabled = NO;
labelView_.drawRect =^(TUIView *view, CGRect rect)
{
[[NSGraphicsContext currentContext] saveGraphicsState];
[[TUIColor whiteColor] set];
[NSBezierPath fillRect:view.bounds];
NSColor *fontColor;
if(weakSelf.isOnline)
fontColor = [NSColor colorWithDeviceWhite:0.2 alpha:1];
else
fontColor = [NSColor colorWithDeviceRed:0.6078 green:0.6510 blue:0.7059 alpha:1];
MVDrawString(weakSelf.fullname ? weakSelf.fullname : weakSelf.email,
view.bounds,
fontColor,
13, kMVStringTypeNormal,
nil, CGSizeMake(0, 0), 0);
[[NSGraphicsContext currentContext] restoreGraphicsState];
};
arrowView_ = [[TUIView alloc] initWithFrame:CGRectMake(0, 0, 24, 30.5)];
arrowView_.userInteractionEnabled = NO;
arrowView_.layout = ^(TUIView *view)
{
CGRect arrowViewFrame = view.frame;
arrowViewFrame.origin.x = weakSelf.bounds.size.width - arrowViewFrame.size.width + 1;
return arrowViewFrame;
};
arrowView_.backgroundColor = [TUIColor colorWithRed:0.1255 green:0.5137 blue:0.9686 alpha:1];
arrowView_.layer.anchorPoint = CGPointMake(1, 0.5);
arrowView_.layer.transform = CATransform3DMakeScale(0.01, 1, 1);
PocketSVG *arrowSvg = [[PocketSVG alloc] initFromSVGFileNamed:@"arrow"];
CAShapeLayer *arrowLayer = [CAShapeLayer layer];
arrowLayer.frame = CGRectMake(8, 58, 6, 10);
arrowLayer.path = [PocketSVG getCGPathFromNSBezierPath:arrowSvg.bezier];
arrowLayer.fillColor = CGColorCreateGenericRGB(1, 1, 1, 1);
arrowLayer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5);
[arrowView_.layer addSublayer:arrowLayer];
CGRect rect = self.bounds;
rect.size.height = 30;
drawView_ = [[TUIView alloc] initWithFrame:rect];
drawView_.autoresizingMask = TUIViewAutoresizingFlexibleWidth;
drawView_.opaque = NO;
drawView_.backgroundColor = [TUIColor clearColor];
drawView_.userInteractionEnabled = NO;
drawView_.drawRect = ^(TUIView *view, CGRect rect)
{
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSColor colorWithDeviceRed:0.8314 green:0.8510 blue:0.8745 alpha:1.0000] set];
[NSBezierPath fillRect:CGRectMake(35, 0, view.bounds.size.width - 35, 0.5)];
[[NSGraphicsContext currentContext] restoreGraphicsState];
};
[self addSubview:drawView_];
[self addSubview:labelView_];
[self addSubview:arrowView_];
[self addSubview:avatarView_];
}
return self;
}
- (void)setNeedsDisplay
{
[self.avatarView setNeedsDisplay];
[self.labelView setNeedsDisplay];
[self.drawView setNeedsDisplay];
}
- (void)redraw
{
[self.drawView redraw];
}
- (void)drawRect:(CGRect)rect
{
}
- (void)layoutSubviews
{
float x = 36;
if (self.isSelected)
x = 38.5;
self.labelView.frame = CGRectMake(x, 6.5, self.bounds.size.width - x - 10, 20);
[super layoutSubviews];
}
- (void)setSelected:(BOOL)s animated:(BOOL)animated
{
[super setSelected:s animated:animated];
float duration = (s ? 0.45 : 0.2);
[TUIView animateWithDuration:duration animations:^{
if(s) {
[TUIView setEasing:[CAMediaTimingFunction functionWithControlPoints:0.2 :2.5 :0.6 :1]];
self.avatarView.layer.transform = CATransform3DMakeScale(1.1818, 1.1818, 1.1818);
}
else {
[TUIView setAnimationCurve:TUIViewAnimationCurveEaseOut];
self.avatarView.layer.transform = CATransform3DIdentity;
}
}];
[TUIView animateWithDuration:duration animations:^{
if(s) {
[TUIView setEasing:[CAMediaTimingFunction functionWithControlPoints:0.28 :1.63 :0.46 :1]];
self.arrowView.layer.transform = CATransform3DMakeScale(1, 1, 1);
}
else {
[TUIView setAnimationCurve:TUIViewAnimationCurveEaseOut];
self.arrowView.layer.transform = CATransform3DMakeScale(0.01, 1, 1);
}
[self layoutSubviews];
}];
}
@end