导航栏渐变文字动画效果(适配iphoneX)

导航栏渐变文字动画效果

话不多说先上图

  • iPhone8的动态图
iPhone8的动态图
  • iPhoneX的动态图
iPhoneX的动态图

完美适配iphoneX

核心代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 计算当前偏移位置
    CGFloat offsetY = scrollView.contentOffset.y;

    NSLog(@"偏移位置===%f",offsetY);
    CGFloat delta = offsetY - _lastOffsetY;
    CGFloat height = kHeardH - delta;
    
    if (height <= NAVIGATION_BAR_HEIGHT) {
        height = NAVIGATION_BAR_HEIGHT;
    }
    [_scaleImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    NSLog(@"delta高度===%f",delta);
    
    //拿到当前centerTextView的位置
    NSLog(@"我当前的位置 x=%f,y=%f,w=%f,h=%f",self.centerTextView.x,self.centerTextView.y,self.centerTextView.width,self.centerTextView.height);
    
    CGFloat margin = kHeardH-NAVIGATION_BAR_HEIGHT;//10是header中Label的top
    
    if (delta > margin) {
        CGPoint center = self.centerTextView.center;
        center.y = NAVIGATION_BAR_HEIGHT - self.centerTextView.height;
        self.centerTextView.center = center;
        self.centerTextView.alpha = 1.0;
    }
    
    
    if (delta <= margin) {
         self.centerTextView.alpha = 0;
    }
    
    if (delta<= 0) {
        CGPoint center = self.centerTextView.center;
        center.y = NAVIGATION_BAR_HEIGHT;
        self.centerTextView.center = center;
        self.centerTextView.alpha = 0.0;
    }
    
   
    
    CGFloat alpha = delta / (kHeardH - NAVIGATION_BAR_HEIGHT);
    if (alpha >= 1.0) {
        alpha = 1.0;
    }
    self.navigationView.alpha = alpha;
    _iconImageView.alpha = 1 - alpha;
    _userNameLabel.alpha = 1 - alpha;

}

点我获取demo:欢迎指正

你可能感兴趣的:(导航栏渐变文字动画效果(适配iphoneX))