iOS实现知乎和途家导航栏渐变的文字动画效果

2020-01-15 18:47:41王旭

三.自定义导航栏及毛玻璃效果及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;
}