不用必须自定义导航栏实现淘宝个人中心名字的移动效果

不用必须自定义导航栏实现淘宝个人中心名字的移动效果_第1张图片
图片发自App

上认识的一个朋友问我的,觉得很有意思就尝试了一下


不用必须自定义导航栏实现淘宝个人中心名字的移动效果_第2张图片
999.gif

实现方法应该有很多种,欢迎留言一起探讨,进步提高

全部代码如下 :

//
//  ViewController.m
//  NameAnimation
//
//  Created by FDC-iOS on 17/3/31.
//  Copyright © 2017年 meilun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () 

@end

@implementation ViewController {
    UILabel * nameLabelBottom;
    UILabel * nameLabelMiddle;
    UILabel * nameLabelTop;
    CGFloat  offsetX;
    UIScrollView * _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBase];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
}



- (void)setBase{
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.contentSize = CGSizeMake(0, 999);
    [self.view addSubview:_scrollView];
    
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2"]];
    imageView.frame = CGRectMake(0, 0, 375, 200);
    [_scrollView addSubview:imageView];
    
    nameLabelBottom = [[UILabel alloc] initWithFrame:CGRectMake(20, 164, 0, 0)];
    nameLabelBottom.text = @"马铃薯蜀黍";
    [nameLabelBottom sizeToFit];
    
    [_scrollView addSubview:nameLabelBottom];
    _scrollView.delegate = self;
    
    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    nameLabelMiddle = [[UILabel alloc] initWithFrame:CGRectMake(20, 164, 0, 0)];
    nameLabelMiddle.text = @"马铃薯蜀黍";
    [nameLabelMiddle sizeToFit];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:nameLabelMiddle];
    
    
    nameLabelTop = [[UILabel alloc] init];//WithFrame:CGRectMake(20, 30, 0, 0)];
    nameLabelTop.text = @"马铃薯蜀黍";
    [nameLabelTop sizeToFit];
    nameLabelTop.hidden = YES;
    nameLabelTop.center = self.navigationController.navigationBar.center;
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:nameLabelTop];
    
    offsetX = ([UIScreen mainScreen].bounds.size.width/2) - nameLabelMiddle.center.x;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.navigationController.navigationBar.alpha = 0;
    });
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offset = scrollView.contentOffset.y ;
//    NSLog(@"%f",offset);
    
    if (offset > 0) { // 赏花隐藏
        
        nameLabelBottom.hidden = YES;
        nameLabelMiddle.hidden =NO;
        CGFloat x = offset/132 * offsetX;
        
        CGRect labelRect = nameLabelMiddle.frame;
        labelRect.origin.x = 20 + x;
        labelRect.origin.y = 164 - offset;
        nameLabelMiddle.frame = labelRect;
        
        self.navigationController.navigationBar.alpha = offset/132.0f;
    }else {
        nameLabelBottom.hidden = NO;
        nameLabelMiddle.hidden = YES;
    }
    
    if (offset > 132 ) {
        nameLabelMiddle.hidden = YES;
        nameLabelTop.hidden = NO;
    }else {
        nameLabelTop.hidden = YES;
    }
}


@end


你可能感兴趣的:(不用必须自定义导航栏实现淘宝个人中心名字的移动效果)