iOS开发-导航栏标题动画

简介: iOS开发-导航栏标题动画

1.png

正常做这种导航栏隐藏一般都是整体改变透明度实现的,觉得太low了,所以就想能不能做的高大上一点,直接飞过去,于是乎就有了这个动画,下面放上代码,下载链接放在文末:

//核心的东西都在scrollView的代理方法里
#import "ViewController.h"
#import "NavAnimaitinCell.h"
#import "UIColor+Hex.h"
#define ks_width [UIScreen mainScreen].bounds.size.width
#define ks_height [UIScreen mainScreen].bounds.size.height
#define knavbar_height (kstatusBarHeight + 44)
#define kstatusBarHeight [UIApplication sharedApplication].statusBarFrame.size.height
#define bottom_distance 60
#define img_height 281 / (450 / ks_width)
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
{
    UITableView *headerAniTableView;
    UIImageView *headerBgImageView;
    UIView *whiteBaseView;
    UIView *hdHeaderView;
    UIView *navigationView;
    UILabel *hotTitle;
    CGSize titlesize;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    headerAniTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ks_width, ks_height) style:UITableViewStylePlain];
    headerAniTableView.delegate = self;
    headerAniTableView.dataSource = self;
    [self.view addSubview:headerAniTableView];
    headerAniTableView.tableHeaderView = [self createNavHeaderView];
    [self creatNavigationView];
}
#pragma mark - creatNavigationView
- (void)creatNavigationView
{
    navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ks_width, knavbar_height)];
    navigationView.backgroundColor = [UIColor whiteColor];
    navigationView.alpha = 0;
    [self.view addSubview:navigationView];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, knavbar_height - 0.5, ks_width, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHex:@"#999999"];
    [navigationView addSubview:lineView];
}
- (UIView *)createNavHeaderView
{
    hdHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ks_width, img_height)];
    hdHeaderView.clipsToBounds = YES;
    headerBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ks_width, img_height)];
    headerBgImageView.userInteractionEnabled = YES;
    headerBgImageView.image = [UIImage imageNamed:@"top_header.jpg"];
    [hdHeaderView addSubview:headerBgImageView];
    whiteBaseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ks_width, img_height)];
    whiteBaseView.userInteractionEnabled = NO;
    whiteBaseView.backgroundColor = [UIColor whiteColor];
    whiteBaseView.alpha = 0;
    [hdHeaderView addSubview:whiteBaseView];
    titlesize =  [@"落红不是无情物" boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size;
    hotTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, img_height - bottom_distance, titlesize.width, 44)];
    hotTitle.font = [UIFont boldSystemFontOfSize:18];
    hotTitle.textColor = [UIColor whiteColor];
    hotTitle.text = @"落红不是无情物";
    [self.view addSubview:hotTitle];
    return hdHeaderView;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NavAnimaitinCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[NavAnimaitinCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float luochaY = img_height - knavbar_height;
    //做了一个颜色的渐变,主要是白和黑的相互转化
    NSArray *colorArray = @[@"#000000", @"#111111", @"#222222", @"#333333", @"#444444", @"#555555", @"#666666", @"#777777", @"#888888", @"#999999", @"#aaaaaa", @"#bbbbbb", @"#cccccc", @"#dddddd", @"#eeeeee", @"#ffffff"];
    CGFloat offSetY = scrollView.contentOffset.y;
    if (offSetY<=0) {
    //下拉的时候的效果,这里不能够下拉,所以这里写不写问题不大,如果想要仔加一个下拉放大的效果,就可以卸载这里,保险起见,博主这里做了保留原设置的处理
        scrollView.contentOffset = CGPointMake(0, 0);
        navigationView.alpha = 0;
        hotTitle.frame = CGRectMake(20, img_height - 60, titlesize.width, 44);
        hotTitle.textColor = [UIColor whiteColor];
        headerBgImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
        whiteBaseView.alpha = 0;
    }
    else if (offSetY > luochaY) {
    //飞过去之后就不再做改变了
        offSetY = luochaY;
        navigationView.alpha = 1;
        whiteBaseView.alpha = 1;
        hotTitle.frame = CGRectMake(20 + offSetY / luochaY * ((ks_width - 40 - titlesize.width) / 2), kstatusBarHeight, titlesize.width, 44);
        hotTitle.textColor = [UIColor blackColor];
    }else{
    //让覆盖的蒙版慢慢变成nav的颜色,此处为白色,标题根据最终位置的差值按照移动的比例一步步变化,造成一种飞过去的感觉
        NSLog(@"%f", offSetY);
        navigationView.alpha = 0;
        hotTitle.frame = CGRectMake(20 + offSetY / luochaY * ((ks_width - 40 - titlesize.width) / 2),img_height - bottom_distance - (img_height - bottom_distance - kstatusBarHeight) * (offSetY / luochaY), titlesize.width, 44);
        int colorIndex = colorArray.count - offSetY / (luochaY / colorArray.count);
        hotTitle.textColor = [UIColor colorWithHex:colorArray[colorIndex]];
        headerBgImageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 + 0.4 * (offSetY / luochaY), 1 + 0.2* (offSetY / luochaY));
        whiteBaseView.alpha = offSetY / luochaY;
    }
    [self.view bringSubviewToFront:hotTitle];
}
//防止scrollview停在渐变的过程中,如果停了,根据偏移量回归原位或者直接过渡
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    CGFloat offSetY = scrollView.contentOffset.y;
    if (offSetY > 60 && offSetY <= 112) {
        [scrollView setContentOffset:CGPointMake(0, img_height - knavbar_height) animated:YES];
    }
    if (offSetY > 0 && offSetY <= 60) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

另外,大家也看到了,这是针对一开始没有导航栏,头部是大图的情况的。


下面是下家最期待的Demo下载地址:点击前往下载

目录
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
8天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
12 0
|
23天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
199 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
28 1
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
93 2