iOS-導航欄滾動

前言微信

搜索功能在app中算是很經常使用的,好比微信的搜索,應該是運用UISearchController實現的。app

 

(一)效果的實現,經過tableview的垂直滾動距離,計算出每次移動的距離,最大移動距離44;ide

(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;
}
相關文章
相關標籤/搜索