在iOS 7中动画UINavigationBar(如Safari) [英] Animating UINavigationBar in iOS 7 (like Safari)

查看:118
本文介绍了在iOS 7中动画UINavigationBar(如Safari)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Safari中滚动内容时,标题栏会动画显示其自身的较小版本。实现这个的最佳方法是什么?

When scrolling content in Safari, the title bar animates to a smaller version of its self. What is the best way to implement this?

目前,我正在改变框架的大小:

Currently, I am changing the size of the frame like so:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //
    //  Table view
    //
    CGFloat currentPosition = scrollView.contentOffset.y - CGRectGetHeight(self.tableView.tableHeaderView.frame) + CGRectGetHeight(self.navigationController.navigationBar.frame) + CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]);

    if ([scrollView isKindOfClass:[HeadlinesHeadlinesTableView class]]) {
        ScrollDirection scrollDirection;

        if (self.lastContentOffset > scrollView.contentOffset.y) {
            scrollDirection = ScrollDirectionDown;
        } else if (self.lastContentOffset < scrollView.contentOffset.y) {
            scrollDirection = ScrollDirectionUp;
        }

        self.lastContentOffset = scrollView.contentOffset.y;

        CGRect frame = self.navigationController.navigationBar.frame;
        CGFloat minimumFrameHeight = 30.0f;
        CGFloat maximumFrameHeight = 44.0f;

        CGFloat titleSize = [[self.navigationController.navigationBar.titleTextAttributes objectForKey:NSFontAttributeName] pointSize];
        CGFloat minimumTitleHeight = 22.0f;
        CGFloat maximumTitleHeight = 30.0f;

        if (currentPosition > 0 && CGRectGetHeight(frame) >= minimumFrameHeight && CGRectGetHeight(frame) <= maximumFrameHeight) {
            switch (scrollDirection) {
                case ScrollDirectionUp:
                    frame.size.height--;
                    titleSize--;
                    break;

                case ScrollDirectionDown:
                    frame.size.height++;
                    titleSize++;
                    break;

                default:
                    break;
            }

            if (CGRectGetHeight(frame) <= minimumFrameHeight) {
                frame.size.height = minimumFrameHeight;
            }

            if (CGRectGetHeight(frame) >= maximumFrameHeight) {
                frame.size.height = maximumFrameHeight;
            }

            if (titleSize <= minimumTitleHeight) {
                titleSize = minimumTitleHeight;
            }

            if (titleSize >= maximumTitleHeight) {
                titleSize = maximumTitleHeight;
            }
        }

        [self.navigationController.navigationBar setFrame:frame];
        [self.navigationController.navigationBar setTitleTextAttributes: @{ NSFontAttributeName : [UIFont fontWithName:@"Canterbury-Regular" size:titleSize] }];
    }
}

当然,这种方式并不顺畅,很多代码,更不用说我需要淡出条形按钮项目。

Naturally, this way is not smooth and a lot of code, not to mention the fact that I need to fade out bar button items as well.

提前致谢!

推荐答案

看看 AMScrollingNavbar ,它已经褪色支持。
您可以尝试更改NavigationBar的字体大小以使标题更小。

Take a look at AMScrollingNavbar, it already has fading support. You can try to change font size of NavigationBar to make the title smaller.

这篇关于在iOS 7中动画UINavigationBar(如Safari)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆