1. 程式人生 > >TTNavigationBar-alpha 導航欄隱藏漸變

TTNavigationBar-alpha 導航欄隱藏漸變

歡迎大家到github下載
https://github.com/TimTian008/TTNavigationBar-alpha
這裡寫圖片描述
TTNavigationBar-alpha

滑動tableview 導航欄漸變,導航欄文字上移 本文所引用的佈局類cocopods進行安裝

pod ‘Masonry’

按照順序新增檢視

#pragma mark-- 生命週期
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"FadeNavigationDemo";
       self.tableView
.rowHeight = 100; self.navigationController.navigationBar.hidden = YES; [self.view addSubview:self.scaleImageView]; // 設定展示圖片的約束 [_scaleImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.equalTo(self.view.mas_left); make.right
.equalTo(self.view.mas_right); make.height.mas_equalTo(kHeardH); }]; //設定自定義導航欄 self.navigationController.navigationBar.hidden = YES; self.lastOffsetY = - kHeardH+35; [self.view addSubview:self.tableView]; self.tableView.backgroundColor = [UIColor clearColor]; [self.view
addSubview:self.navigationView]; self.navigationController.navigationBar.barStyle = UIBarStyleBlack; // 直接新增到控制器的View上面,注意新增順序,在新增導航欄之後,否則會被遮蓋住 [self configNavigationBar]; }

建立自定義導航檢視 // 注意:毛玻璃效果API是iOS8的,適配iOS8以下的請用其他方法

// 注意:毛玻璃效果API是iOS8的,適配iOS8以下的請用其他方法
-(void)setNavigationSubView{
    // 毛玻璃背景
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:_navigationView.bounds];
    bgImgView.image = [UIImage imageNamed:@"DialBackground"];
    [_navigationView addSubview:bgImgView];
    /**  毛玻璃特效型別
     *   UIBlurEffectStyleExtraLight,
     *   UIBlurEffectStyleLight,
     *   UIBlurEffectStyleDark
     */
    UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];    //  毛玻璃檢視
    UIVisualEffectView * effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];    //新增到要有毛玻璃特效的控制元件中
    effectView.frame = bgImgView.bounds;
    [bgImgView addSubview:effectView];    //設定模糊透明度
    effectView.alpha = 0.9f;    //中間文字框
    UIView *centerTextView = [[UIView alloc]init];
    self.centerTextView = centerTextView;
    CGFloat centerTextViewX = 0;
    CGFloat centerTextViewY = 20;
    CGFloat centerTextViewW = 0;
    CGFloat centerTextViewH = 0;    //文字大小
    NSString *title = @"FadeNavigationDemo";
    NSString *desc  = @"timtian_desc";
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    CGSize descSize = [desc sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]}];
    centerTextViewW = titleSize.width > descSize.width ? titleSize.width : descSize.width;
    centerTextViewH = titleSize.height + descSize.height +10;
    centerTextViewX = (__kScreenWidth - centerTextViewW) / 2;
    centerTextView.frame = CGRectMake(centerTextViewX, centerTextViewY, centerTextViewW, centerTextViewH);    //文字label

    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:12];
     titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0,5, centerTextViewW, titleSize.height);    UILabel *descLabel = [[UILabel alloc]init];
    descLabel.textAlignment = NSTextAlignmentCenter;
    descLabel.text = desc;
    descLabel.font = [UIFont systemFontOfSize:11];
    descLabel.textColor = [UIColor whiteColor];
    descLabel.frame = CGRectMake(0, titleSize.height + 5, centerTextViewW, descSize.height);
    [centerTextView addSubview:titleLabel];
    [centerTextView addSubview:descLabel];
    [_navigationView addSubview:centerTextView];
}

滾動導航漸變程式碼

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 計算當前偏移位置
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat delta = offsetY - _lastOffsetY;
    NSLog(@"delta=%f",delta);
    NSLog(@"offsetY=%f",offsetY);
    CGFloat height = kHeardH - delta;
    if (height < kNavBarH) {
        height = kNavBarH;
    }

//    navigationTitile 位移相關程式碼
    CGFloat margin = 205;


    if (delta > margin && delta < margin+39) {

        self.centerTextView.y = 64 - (delta-margin);
        self.centerTextView.alpha = 1.0;
    }
    if (delta>margin+39) {

        self.centerTextView.y = 25;
        self.centerTextView.alpha = 1.0;
    }
    if (delta<=margin) {
        self.centerTextView.alpha = 0;
    }
    if (delta<= 0) {
        self.centerTextView.y = 64;
        self.centerTextView.alpha = 0.0;
    }
    [_scaleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    CGFloat alpha = delta / (kHeardH - kNavBarH);
    if (alpha >= 1.0) {
        alpha = 1.0;
    }
    self.navigationView.alpha = alpha;
}