iOS實現知乎和途家導覽列漸層的文字動畫效果_IOS

來源:互聯網
上載者:User

效果圖如下

分析如下:

     1.導覽列一開始是隱藏的,隨著scrollView滾動而漸層

     2.導覽列左右兩邊的navigationItem是一直顯示的

     3.導覽列參考了途家app,使用了毛半透明效果,背景是一張圖片

     4.下拉放大圖片效果

     5.title文字動畫效果

通過簡單分析,系統的導覽列實現以上效果有點困難,直接自訂一個假的導覽列更容易點

分布拆解實現以上效果

一.下拉放大header圖片

- (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; DLog(@"delta=%f",delta); DLog(@"offsetY=%f",offsetY); 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是iOS8的,適配iOS8以下的請用其他方法-(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; CGFloat centerTextViewX = 0; CGFloat centerTextViewY = 64; CGFloat centerTextViewW = 0; CGFloat centerTextViewH = 0; //文字大小 NSString *title = @"Pg.lostk開啟後搖滾的新圖景"; NSString *desc = @"搖滾清心坊8套"; 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 = (SCREENWIDTH - 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];}聲明控制項@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; DLog(@"delta=%f",delta); DLog(@"offsetY=%f",offsetY); CGFloat height = kHeardH - delta; if (height < kNavBarH) {  height = kNavBarH; } 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;}

總結

以上就是這篇文章的全部內容,希望對大家的學習或者工作帶來一定的協助,如果有疑問大家可以留言交流。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.