iOS 知识点iOS Developer

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

2017-04-01  本文已影响196人  马铃薯蜀黍
不用必须自定义导航栏实现淘宝个人中心名字的移动效果

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


999.gif

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

全部代码如下 :

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

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

@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


上一篇下一篇

猜你喜欢

热点阅读