iOS-导航栏滚动

本文介绍了一种在iOS应用中实现搜索视图动画的方法。通过计算UITableView的滚动距离,控制UISearchBar和导航栏的过渡动画,实现了平滑的搜索栏展开效果。文章详细展示了如何通过Swift代码自定义UISearchBar样式并实现导航栏透明度变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

搜索功能在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;
}

 

转载于:https://my.oschina.net/u/1450995/blog/822204

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值