导航条根据UITableView或者UICollectionView的滑动变透明或者不透明

懒癌晚期,实在懒得写了,只把效果和代码放上来了~

//
//  MallRecommendViewController.m
//  DiDong
//
//  Created by 岳霖 on 2017/10/9.
//  Copyright © 2017年 岳霖. All rights reserved.
//

#import "MallRecommendViewController.h"

#import "MallHeaderView.h"
#import "ProductsCell.h"
#import "MallSearchHistoryViewController.h"

@interface MallRecommendViewController () <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate> {
    UIImageView *_barImageView;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UICollectionReusableView *headerView;

@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UIView *fakeNavigationBar;
//@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIImageView *searchIcon;

#define SearchTfBgColor_Deep [UIColor colorWithHex:0xf7f7f7]
#define SearchTfBgColor_Low  [UIColor colorWithWhite:1 alpha:0.2]
#define PlaceHolderColor_Deep [UIColor colorWithHex:0xc5c5c5]
#define PlaceHolderColor_Low [UIColor whiteColor]
#define SearchIcon_Deep @"mall_sousuo_huise"
#define SearchIcon_Low @"mall_sousuo_icon"
#define BackIcon_Deep @"fanhuiheise_shouye"
#define BackIcon_Low @"fanhui_shouye"

#define BannerHeight 230

@end

@implementation MallRecommendViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    self.navigationController.navigationBar.hidden = YES;
    //    [self setFakeNavigationBar];
//    CGFloat alpha = self.collectionView.contentOffset.y / 64.f;
//    _barImageView.alpha = alpha;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"%f",_barImageView.alpha);
    CGFloat alpha = self.collectionView.contentOffset.y / 64.f;
    _barImageView.alpha = alpha;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-64);
        make.left.and.right.and.bottom.mas_equalTo(0);
    }];

    _barImageView = self.navigationController.navigationBar.subviews.firstObject;
    NSLog(@"%f",_barImageView.alpha);
    //iOS11下不能正确设置alpha为0.经过测试打印,在viewDidAppear之前,_barImageView的frame都是从0开始,alpha可以设置为0,但是并不能变成透明,到viewDidAppear方法中就从-20开始,alpha自动变成1,网上查资料得出来结果是在主线程中设置,测试有效
    if (@available (iOS 11, *)) {
        dispatch_async(dispatch_get_main_queue(), ^{
            _barImageView.alpha = 0;
        });
    }
    NSLog(@"%f",_barImageView.alpha);
}

#pragma mark 设置导航条
- (void)setNavigationBar {
    [super setNavigationBar];
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:BackIcon_Low] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor separatorColor]]];
    self.navigationItem.titleView = self.searchTf;
}

#pragma mark 重写返回方法,让tabbar返回
- (void)leftItemAction {
    [self.parentViewController.navigationController popViewControllerAnimated:NO];
}

/*
 #pragma mark 设置假的导航条
 - (void)setFakeNavigationBar{
 [self.view addSubview:self.fakeNavigationBar];
 [self.fakeNavigationBar addSubview:self.searchTf];
 [self.fakeNavigationBar addSubview:self.backButton];
 
 [self.fakeNavigationBar mas_updateConstraints:^(MASConstraintMaker *make) {
 make.left.and.top.and.right.mas_equalTo(0);
 make.height.mas_equalTo(64);
 }];
 
 [self.backButton mas_updateConstraints:^(MASConstraintMaker *make) {
 make.left.mas_equalTo(0);
 make.right.equalTo(self.searchTf.mas_left);
 make.top.and.bottom.equalTo(self.searchTf);
 }];
 
 [self.searchTf mas_updateConstraints:^(MASConstraintMaker *make) {
 make.left.mas_equalTo(35);
 make.top.mas_equalTo(27);
 make.right.mas_equalTo(-15);
 make.height.mas_equalTo(30);
 }];
 }
 
 #pragma mark 初始化假的导航条
 - (UIView *)fakeNavigationBar {
 if (!_fakeNavigationBar) {
 _fakeNavigationBar = [[UIView alloc] init];
 _fakeNavigationBar.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
 }
 return _fakeNavigationBar;
 }
 
 #pragma mark 初始化返回按钮
 - (UIButton *)backButton {
 if (!_backButton) {
 _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [_backButton setImage:[UIImage imageNamed:BackIcon_Low] forState:UIControlStateNormal];
 [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
 }
 return _backButton;
 }
 
 - (void)backAction {
 [self.parentViewController.navigationController popViewControllerAnimated:YES];
 }
 */

#pragma mark 初始化搜索框
- (UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        _searchTf.delegate = self;
        _searchTf.placeholder = @"搜索商品";
        _searchTf.borderStyle = UITextBorderStyleNone;
        _searchTf.layer.masksToBounds = YES;
        _searchTf.layer.cornerRadius = 3;
        _searchTf.layer.borderColor = [UIColor clearColor].CGColor;
        _searchTf.font = [UIFont systemFontOfSize:12];
        _searchTf.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, 11, 12.5)];
        imageView.image = [UIImage imageNamed:SearchIcon_Low];
        [leftView addSubview:imageView];
        self.searchIcon = imageView;
        _searchTf.leftView = leftView;
    }
    return _searchTf;
}

#pragma mark UITextFieldDelegat,点击textfield的时候推出新页面
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    MallSearchHistoryViewController *searchHistory = [[MallSearchHistoryViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:searchHistory];
    navi.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    navi.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:navi animated:YES completion:nil];
    return NO;
}

#pragma mark 初始化collectionView
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor separatorColor];
        layout.itemSize = CGSizeMake((SCREEN_WIDTH-0.5)/2, 191 * autoSizeScaleX);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ProductsCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[MallHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}

#pragma UICollectionViewFlowLayout,UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.5;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.5, 0, 0.5, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, BannerHeight);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //        if (!header) {
        //            header = [[MallHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, BannerHeight)];
        //        }
        return header;
    } else {
        return [[UICollectionReusableView alloc] init];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat minAlphaOffset = 0;
    CGFloat maxAlphaOffset = 64;
    CGFloat offset = scrollView.contentOffset.y;
    CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
    //    self.fakeNavigationBar.backgroundColor = [UIColor colorWithWhite:1 alpha:alpha];
    _barImageView.alpha = alpha;
    
    if (scrollView.contentOffset.y > 32) {
        [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:BackIcon_Deep] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        //        [self.backButton setImage:[UIImage imageNamed:BackIcon_Deep] forState:UIControlStateNormal];
        [self.searchIcon setImage:[UIImage imageNamed:SearchIcon_Deep]];
        self.searchTf.backgroundColor = SearchTfBgColor_Deep;
        [self.searchTf setValue:PlaceHolderColor_Deep forKeyPath:@"_placeholderLabel.textColor"];
    } else {
        [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:BackIcon_Low] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        //        [self.backButton setImage:[UIImage imageNamed:BackIcon_Low] forState:UIControlStateNormal];
        [self.searchIcon setImage:[UIImage imageNamed:SearchIcon_Low]];
        self.searchTf.backgroundColor = SearchTfBgColor_Low;
        [self.searchTf setValue:PlaceHolderColor_Low forKeyPath:@"_placeholderLabel.textColor"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */



@end

 

posted @ 2017-10-18 13:45  YouNeedCourage  阅读(1134)  评论(0编辑  收藏  举报