美文网首页iOS 知识点iOS Developer
不用必须自定义导航栏实现淘宝个人中心名字的移动效果

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

作者: 马铃薯蜀黍 | 来源:发表于2017-04-01 00:28 被阅读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


相关文章

网友评论

  • Casablanca1Q84S:我贡献一下我怎么写的,只用一个lablel

    if (!self.navNameLalbe) {
    self.navNameLalbe = [[UILabel alloc]initWithFrame:CGRectZero];
    self.navNameLalbe.font = SYS_Font_Medium(18);
    self.navNameLalbe.textColor = [UIColor whiteColor];

    CGFloat width = [NSString widthOfString:self.carInfoDetail.defaultCar.SerialShowName font:self.navNameLalbe.font];
    CGFloat height = [NSString heightOfString:self.carInfoDetail.defaultCar.SerialShowName width:width font:self.navNameLalbe.font];

    self.navNameLalbe.frame = CGRectMake(0, 0 , width,height);
    self.navNameLalbe.text = self.carInfoMationView.carName.text;
    [self.navigationController.navigationBar addSubview:self.navNameLalbe];
    //目的位置中心
    self.destinationCenter = CGPointMake(self.view.centerX, 22);
    //起始目的中心
    self.originalCenter = CGPointMake(20 + width/2 ,kHeaderHeight - 50 - height/2);
    //起始Y距离
    self.originalHeight = self.originalCenter.y - self.destinationCenter.y;
    //起始X距离
    self.originalWidth = self.originalCenter.x - self.destinationCenter.x;
    self.navNameLalbe.center = self.originalCenter;
    }

    Casablanca1Q84S:@马铃薯蜀黍 哈哈哈哈
    马铃薯蜀黍:@Casablanca1Q84S :smile:
    Casablanca1Q84S:滚动代理方法里面
    、-(void)setNagtionBarTitleAnimate:(CGFloat)offset
    {
    if (offset > 0 && offset < self.originalHeight) {
    CGFloat scale = offset/self.originalHeight;
    CGFloat Y = self.originalCenter.y - offset;
    CGFloat X = self.originalCenter.x - scale * self.originalWidth;
    self.navNameLalbe.center = CGPointMake(X,Y);
    if (Y<=44+self.navNameLalbe.height/2) {
    self.navNameLalbe.textColor = [UIColor blackColor];
    }else{
    self.navNameLalbe.textColor = [UIColor whiteColor];
    }
    }else if (offset > self.originalHeight){
    self.navNameLalbe.center = self.destinationCenter;
    self.navNameLalbe.textColor = [UIColor blackColor];
    }else if(offset <= 0){
    CGFloat Y = self.originalCenter.y - offset;
    CGFloat X = self.originalCenter.x;
    self.navNameLalbe.center = CGPointMake(X, Y);
    self.navNameLalbe.textColor = [UIColor whiteColor];
    }
    }

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

本文链接:https://www.haomeiwen.com/subject/xvvbottx.html