您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

iOS 仿知乎頭部效果:自訂導航欄與下拉縮放完整拆解

編輯:iOS 開發基礎


效果如上,先做一次簡單分析

  • 1. 導航欄初始隱藏,隨著 scrollView 滾動而漸顯

  • 2. 導航欄左右兩側的 navigationItem 全程顯示

  • 3. 導航欄參考途家 App,採用毛玻璃效果,背景是一張圖片

  • 4. 頂部圖片支援下拉放大

  • 5. title 文字具備動畫效果

用系統導航欄硬做以上效果會相當吃力,直接自訂一個導航欄反而更省事。

分步拆解實作

一、頂部圖片下拉放大

- (void)viewDidLoad {
    [super viewDidLoad];
    [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);
    }];
}

// tableView 懶載入
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.contentInset = UIEdgeInsetsMake(kHeardH - 35, 0, 0, 0);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

// 圖片懶載入
- (UIImageView *)scaleImageView {
    if (!_scaleImageView){
        _scaleImageView = [[UIImageView alloc] init];
        _scaleImageView.contentMode = UIViewContentModeScaleAspectFill;
        _scaleImageView.clipsToBounds = YES;
        _scaleImageView.image = [UIImage imageNamed:@"666"];
    }
    return _scaleImageView;
}

// 導航欄高度
#define kNavBarH 64.0f
// 頭部圖片高度
#define kHeardH  260

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 計算當前偏移量
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat delta = offsetY - _lastOffsetY;

    CGFloat height = kHeardH - delta;
    if (height < kNavBarH) {
        height = kNavBarH;
    }

    [_scaleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

二、左右兩側的 navigationItem 全程顯示

- (void)viewDidLoad {
    [super viewDidLoad];
    // 直接加到控制器的 view 上,注意添加順序要在導航欄之後,否則會被遮蓋
    [self configNavigationBar];
}

- (void)configNavigationBar{
    // 左側返回按鈕
    UIButton *backBtn = [[UIButton alloc] init];
    backBtn.frame = CGRectMake(0, 20, 44, 44);
    [backBtn setImage:[UIImage imageNamed:@"special_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    // 右側分享按鈕
    UIButton *shartBtn = [[UIButton alloc] init];
    shartBtn.frame = CGRectMake(SCREENWIDTH - 44, 20, 44, 44);
    [shartBtn setImage:[UIImage imageNamed:@"special_share"] forState:UIControlStateNormal];
    [shartBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:backBtn];
    [self.view addSubview:shartBtn];
}

// 返回
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

三、自訂導航欄、毛玻璃與 title 文字動畫

// 隱藏系統導航欄
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    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;
}

// 自訂導航欄
- (UIView *)navigationView{
    if (_navigationView == nil){
        _navigationView = [[UIView alloc] init];
        _navigationView.frame = CGRectMake(0, 0, SCREENWIDTH, kNavBarH);
        _navigationView.backgroundColor = [UIColor clearColor];
        _navigationView.alpha = 0.0;
        // 加入子控件
        [self setNavigationSubView];
    }
    return _navigationView;
}

// 注意:毛玻璃 API 為 iOS 8 起提供,若需支援 iOS 8 以下請改用其他方案
- (void)setNavigationSubView{
    // 毛玻璃背景
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:_navigationView.bounds];
    bgImgView.image = [UIImage imageNamed:@"666"];
    [_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;

    // 依文字內容計算尺寸
    NSString *title = @"Pg.lostk開啟後搖滾的新圖景";
    NSString *desc  = @"搖滾清心坊8套";
    CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    CGSize descSize  = [desc sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]}];

    CGFloat centerTextViewW = titleSize.width > descSize.width ? titleSize.width : descSize.width;
    CGFloat centerTextViewH = titleSize.height + descSize.height + 10;
    CGFloat centerTextViewX = (SCREENWIDTH - centerTextViewW) / 2;
    centerTextView.frame = CGRectMake(centerTextViewX, 64, centerTextViewW, centerTextViewH);

    // 標題
    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];
}

屬性宣告:

@property (nonatomic, strong) UIView *navigationView;    // 導航欄
@property (nonatomic, strong) UIView *centerTextView;    // title 文字
@property (assign, nonatomic) CGFloat lastOffsetY;       // 記錄上一次位置
@property (nonatomic, strong) UIImageView *scaleImageView; // 頂部圖片

核心程式碼

#pragma mark - ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 計算當前偏移量
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat delta = offsetY - _lastOffsetY;

    CGFloat height = kHeardH - delta;
    if (height < kNavBarH) {
        height = kNavBarH;
    }

    CGFloat margin = 205;
    if (delta > margin && delta < margin + 39) {
        self.centerTextView.y = 25;
        self.centerTextView.alpha = 1.0;
    }
    if (delta <= margin) {
        self.centerTextView.alpha = 0;
    }

    CGFloat alpha = (offsetY + kHeardH - 35) / (kHeardH - 64);
    if (alpha >= 1.0) {
        alpha = 1.0;
    }
    self.navigationView.alpha = alpha;
}

由於是直接寫在專案裡,沒有提供可下載的 Demo,抱歉。若有更好的實作方式,歡迎指教,一起共勉。

文章轉自 船長_ 的簡書

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved