前言
搜索功能在app中算是很常用的,比如微信的搜索,应该是运用UISearchController实现的。
(一)效果的实现,通过tableview的垂直滚动距离,计算出每次移动的距离,最大移动距离44;
(1)关键的逻辑在改委托方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView中实现,通过当前的offsetY与lastOffsetY作比较判断是向上滚动还是向下滚动,再计算出当前的progress(最大=1),最后计算offsetY = 44 * progress计算出移动的距离。
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"测试视图";
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), 44)];
searchBar.barStyle = UISearchBarStyleDefault;
searchBar.backgroundImage = [self GetImageWithColor:[UIColor redColor] andHeight:44];
searchBar.placeholder = @"其实我就是在测试";
searchBar.searchTextPositionAdjustment = UIOffsetMake(10, 0);
[searchBar setSearchFieldBackgroundImage:[self GetImageWithColor:[UIColor whiteColor] andHeight:30] forState:UIControlStateNormal];
UITextField *searchText = [searchBar valueForKey:@"_searchField"];
searchText.leftView = nil;
searchText.background = [self GetImageWithColor:[UIColor whiteColor] andHeight:30];
// UILabel *textFieldInsideSearchBarLabel = [searchText valueForKey:@"_placeholderLabel.textColor"];
// 设置placeHolder的颜色
[searchText setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
// [searchText setValue:@(NSTextAlignmentLeft) forKeyPath:@"_placeholderLabel.textAlignment"];
//textFieldInsideSearchBarLabel.textAlignment = NSTextAlignmentLeft;
testTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
testTable.dataSource = self;
testTable.delegate = self;
testTable.backgroundColor = [UIColor clearColor];
testTable.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
//[self.view addSubview:testView];
[self.view addSubview:testTable];
[self.view addSubview:searchBar];
}
// 生成背景图
- (UIImage *)GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
CGRect r = CGRectMake(0.0f, 0.0f, 1.0f, height);
UIGraphicsBeginImageContext(r.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, r);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
//滚动委托
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//[self.stretchableTableHeader scrollViewDidScroll:scrollView isAutomaticallyAdjustsScrollViewInsets:self.automaticallyAdjustsScrollViewInsets];
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat value = offsetY - lastOffsetY;
CGFloat newValue = fabs(value);
if (offsetY > 0) {
if (isScrollUp) {
CGFloat progress = js_progress + newValue / 64;
js_progress = MIN(1, MAX(0, progress));
} else {
CGFloat progress = js_progress - newValue / 64;
js_progress = MAX(0, MIN(1, progress));
}
if (offsetY > lastOffsetY) {
isScrollUp = YES;
} else {
isScrollUp = NO;
}
} else {
js_progress = 0;
}
lastOffsetY = offsetY;
[self transionForScroll:js_progress];
}
// 移动navigationBar、searchBar以及设置titleView的alpha值
- (void)transionForScroll:(CGFloat)progress {
CGFloat offsetY = 44 * progress;
self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation(0, -offsetY);
searchBar.transform = CGAffineTransformMakeTranslation(0, -offsetY);
NSLog(@"titleView -- %@", [(UIView *)self.navigationController.navigationBar valueForKey:@"_titleView"]);
[[self.navigationController.navigationBar subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
obj.alpha = 1 - progress;
*stop = YES;
}
}];
}
// 然后视图消失,还原navigationBar、searchBar
- (void)viewDidDisappear:(BOOL)animated {
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
searchBar.transform = CGAffineTransformIdentity;
}