-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchBar.m
345 lines (301 loc) · 12.4 KB
/
SearchBar.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// SearchBar.m
// ModalGroupContacts
//
// Created by marco on 13-3-10.
// Copyright (c) 2013年 marco. All rights reserved.
//
#import "SearchBar.h"
#import "ModalGroupController.h"
#import "ICRecipientPicker.h"
#define SEARCHBAR_CONTENT_HEIGHT 44
@interface SearchBar()<UIGestureRecognizerDelegate>
{
CAGradientLayer* _backgroundLayer;
NSArray* _items;
UIButton* _searchBtn;
UITextField* _searchInput;
UIButton* _conformBtn;
UITableView* _searchResultTable;
BOOL _isopen;
}
@property(nonatomic, retain) UIView* actionPickerView;
@property(nonatomic, readonly) UIButton* searchBtn;
@property(nonatomic, readonly) UITextField* searchInput;
@property(nonatomic, readonly) UIButton* conformBtn;
@property(nonatomic, readonly) UITableView* searchResultTable;
@end
@implementation SearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 320, 50)];
if (self) {
[self setup];
}
return self;
}
-(void)setup{
self.backgroundColor = [UIColor clearColor];
[self conformBtn];
[self titleLabel];
_backgroundLayer = [self createBackgroundLayer];
[self.actionPickerView.layer addSublayer:_backgroundLayer];
[self.actionPickerView addSubview:self.searchBtn];
[self.actionPickerView addSubview:self.searchInput];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keboardHiden:) name:UIKeyboardWillHideNotification object:nil];
_selectedItem = [[NSMutableArray array] retain];
[self fillTestDataForTest:_selectedItem];
_isopen = NO;
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_items release];
[_selectedItem release];
[super dealloc];
}
-(UIView*)actionPickerView{
if(!_actionPickerView){
_actionPickerView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width - 40.0f, 7.0f, 30.0f, 30.0f)];
_actionPickerView.layer.cornerRadius = 15.0f;
_actionPickerView.layer.borderWidth = 1.0f;
_actionPickerView.layer.borderColor = [UIColor darkGrayColor].CGColor;
_actionPickerView.clipsToBounds = YES;
[self addSubview:_actionPickerView];
[_actionPickerView release];
}
return _actionPickerView;
}
-(UILabel*)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0f, 12.0f, self.frame.size.width - 70.0f, 20.0f)];
_titleLabel.font = [UIFont systemFontOfSize:18.0f];
_titleLabel.numberOfLines = 0;
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.shadowColor = [UIColor grayColor];
_titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
_titleLabel.hidden = NO;
_titleLabel.opaque = NO;
[self addSubview:_titleLabel];
[_titleLabel release];
}
return _titleLabel;
}
-(UIButton*)searchBtn{
if(!_searchBtn) {
_searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_searchBtn addTarget:self action:@selector(reverseState) forControlEvents:UIControlEventTouchUpInside];
[_searchBtn setImage:[UIImage imageNamed:@"UIButtonBarSearch"] forState:UIControlStateNormal];
_searchBtn.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
_searchBtn.imageEdgeInsets = UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f);
_searchBtn.center = CGPointMake(15.0f, 15.0f);
}
return _searchBtn;
}
-(UITextField*)searchInput{
if (!_searchInput) {
_searchInput = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 265, 20)];
_searchInput.font = [UIFont systemFontOfSize:14];
_searchInput.textColor = [UIColor whiteColor];
_searchInput.center = CGPointMake(168, 16.f);
_searchInput.placeholder = @"请输入搜索的内容";
_searchInput.autocorrectionType = UITextAutocorrectionTypeNo;
_searchInput.clearButtonMode = UITextFieldViewModeAlways;
[_searchInput autorelease];
}
return _searchInput;
}
// here need three20. UI
-(UIButton*)conformBtn{
if (!_conformBtn) {
_conformBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_conformBtn.frame = CGRectMake(15, 7, 40, 30);
[_conformBtn addTarget:self action:@selector(btnConformClick) forControlEvents:UIControlEventTouchUpInside];
[_conformBtn setTitle:@"确定" forState:UIControlStateNormal];
[self addSubview:_conformBtn];
}
return _conformBtn;
}
-(UITableView*)searchResultTable{
if (!_searchResultTable) {
_searchResultTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
[self.superViewController.view insertSubview:_searchResultTable belowSubview:self.superViewController.picker];
_searchResultTable.dataSource = self;
_searchResultTable.delegate = self;
[_searchResultTable release];
}
return _searchResultTable;
}
-(CAGradientLayer*)createBackgroundLayer{
CAGradientLayer* backgroundLayer = [CAGradientLayer layer];
backgroundLayer.anchorPoint = CGPointMake(0.0f, 0.0f);
backgroundLayer.position = CGPointMake(0.0f, 0.0f);
backgroundLayer.startPoint = CGPointZero;
backgroundLayer.endPoint = CGPointMake(0.0f, 1.0f);
backgroundLayer.colors = [NSArray arrayWithObjects:(id)[UIColor grayColor].CGColor, (id)[UIColor darkGrayColor].CGColor, nil];
backgroundLayer.bounds = CGRectMake(0.0f, 0.0f, self.frame.size.width - 20.0f, 30.0f);
return backgroundLayer;
}
- (void)drawRect:(CGRect)rect {
static CGFloat colors[] = { //content background color
131.0f / 255.0f, 168.0f / 255.0f, 200.0f / 255.0f, 1.0f,
39.0f / 255.0f, 92.0f / 255.0f, 150.0f / 255.0f, 1.0f
};
[self drawLinearGradientInRect:CGRectMake(0.0f, 0.0f, rect.size.width, 44.0f) colors:colors];
// bottom line board
CGFloat line2[]={94.0f / 255.0f, 103.0f / 255.0f, 109.0f / 255.0f, 1.0f};
[self drawLineInRect:CGRectMake(0.0f, 44.5f, rect.size.width, 0.0f) colors:line2];
// shadow
CGFloat colors2[] = {
152.0f / 255.0f, 156.0f / 255.0f, 161.0f / 255.0f, 0.5f,
152.0f / 255.0f, 156.0f / 255.0f, 161.0f / 255.0f, 0.1f
};
[self drawLinearGradientInRect:CGRectMake(0.0f, 45.0f, rect.size.width, 5.0f) colors:colors2];
}
- (void)drawLinearGradientInRect:(CGRect)rect colors:(CGFloat[])colours {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colours, NULL, 2);
CGColorSpaceRelease(rgb);
CGPoint start, end;
start = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height * 0.25);
end = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height * 0.75);;
CGContextClipToRect(context, rect);
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
CGContextRestoreGState(context);
}
- (void)drawLineInRect:(CGRect)rect colors:(CGFloat[])colors {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetRGBStrokeColor(context, colors[0], colors[1], colors[2], colors[3]);
CGContextSetLineCap(context, kCGLineCapButt);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
//- (void)handleActionPickerViewTap:(UIGestureRecognizer *)gestureRecognizer {}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
for (UIView *subview in self.actionPickerView.subviews) {
if (subview == touch.view && self.titleLabel.isHidden) {
return NO;
}
}
[self open];
return YES;
}
- (void)shrinkActionPicker {
self.titleLabel.hidden = NO;
[self setNeedsLayout];
}
-(void)open{
if (_isopen) {
return;
}
_isopen = YES;
[self.searchInput becomeFirstResponder];
// __block __typeof__(self) blockSelf = self;
[UIView animateWithDuration:0.2
animations:^{
_actionPickerView.frame = CGRectMake(10.0f, 7.0f, self.frame.size.width - 20.0f, 30.0f);
[_titleLabel setAlpha:0.0f];
}];
}
-(void)close{
if (!_isopen) {
return;
}
_isopen = NO;
self.searchInput.text = nil;
[self.searchInput resignFirstResponder];
// __block __typeof__(self) blockSelf = self;
[UIView animateWithDuration:0.2
animations:^{
_actionPickerView.frame = CGRectMake(self.frame.size.width - 40.0f, 7.0f, 30.0f, 30.0f);
[_titleLabel setAlpha:1.0f];
}];
}
-(void)reverseState{
if (_isopen) {
[self close];
}else{
[self open];
}
}
-(void)btnConformClick{
[self.superViewController.navigationController popViewControllerAnimated:YES];
}
-(void)fillTestDataForTest:(NSMutableArray*) inoutArg{
[inoutArg addObject:@"item1"];
[inoutArg addObject:@"item2"];
[inoutArg addObject:@"item3"];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _selectedItem.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// test logic
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"searchBarResultCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] init] autorelease];
}
cell.textLabel.text = [_selectedItem objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self close];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"item be deselected %@", indexPath);
}
#pragma mark - notification center event handler.
-(void)keboardWillShow:(NSNotification *)info{
if (_isopen) {
ICRecipientPicker* pv = self.superViewController.picker;
if ([pv.datasource numberOfItemsInPikcerView:pv] > 0 && pv.frame.origin.y < SEARCHBAR_CONTENT_HEIGHT) {
float tbHeight = self.superViewController.view.frame.size.height - SEARCHBAR_CONTENT_HEIGHT - pv.frame.size.height;
// self.searchResultTable.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT + pv.frame.size.height, self.frame.size.width, tbHeight);
self.searchResultTable.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT, self.frame.size.width, tbHeight);
[UIView animateWithDuration:0.2 animations:^{
pv.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT, pv.frame.size.width, pv.frame.size.height);
}];
} else {
float tbHeight = self.superViewController.view.frame.size.height - SEARCHBAR_CONTENT_HEIGHT;
self.searchResultTable.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT, self.frame.size.width, tbHeight);
}
}
}
-(void)keboardDidShow:(NSNotification *)info{
if (_isopen) {
CGRect keyboardBounds;
[[info.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
ICRecipientPicker* pv = self.superViewController.picker;
[UIView beginAnimations:nil context:nil];
if ([pv.datasource numberOfItemsInPikcerView:pv] > 0) {
self.searchResultTable.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT + pv.frame.size.height, self.frame.size.width, self.superViewController.view.frame.size.height - keyboardBounds.size.height - pv.frame.size.height);
}else{
self.searchResultTable.frame = CGRectMake(0, SEARCHBAR_CONTENT_HEIGHT, self.frame.size.width, self.superViewController.view.frame.size.height - keyboardBounds.size.height - SEARCHBAR_CONTENT_HEIGHT);
}
[UIView commitAnimations];
}
}
-(void)keboardHiden:(NSNotification *)note{
self.searchResultTable.frame = CGRectZero;
}
@end