在Facebook iOS应用程序的时间轴中添加浮动栏到滚动视图 [英] Add a floating bar to a scroll view like in the Facebook iOS app's timeline

查看:74
本文介绍了在Facebook iOS应用程序的时间轴中添加浮动栏到滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的测试项目添加不同的交互,我无法添加像Facebook的帖子状态栏,位于时间轴滚动视图上的那个,当你'时滚动滚动视图向上滚动视图但在向上滚动时仍然卡在导航栏下。

I've been playing around with adding different interactions to a test project of mine, and I'm having trouble adding something like Facebook's post status bar, the one that sits on the timeline scrollview and scrolls with the scrollview when you're scrolling down the view but stays stuck under the navbar when you're scrolling up it.

我一直在创建一个单独的UIViewController(不是UIView)并将其添加为主ViewController的子视图。我不太确定从那里去哪里...新视图如何滚动滚动视图?我是否应该使用单独的viewcontroller?

I've been creating a separate UIViewController (not UIView) and adding it as a subview to the main ViewController. I'm not exactly too sure where to go from there though... How does the new view scroll with the scrollview? Should I even be using a separate viewcontroller?

任何帮助将不胜感激!谢谢!

Any help would be greatly appreciated! Thanks!

推荐答案

下面是一些可用于入门的示例代码,只需将其添加到视图控制器即可。它为浮动栏使用通用的 UIView ,为滚动视图使用通用的 UIScrollView ,但是您可以将其更改为你想要的。

Here's some sample code you can use to get started, just add it to your view controller. It uses a generic UIView for the floating bar and a generic UIScrollView for the scroll view but you can change that to whatever you want.

@interface BNLFDetailViewController () <UIScrollViewDelegate> {
    UIScrollView *_scrollView;
    UIView *_floatingBarView;
    CGFloat _lastOffset;
}
@end

并且在中@实现添加:

- (void)viewDidLoad {
    [super viewDidLoad];

    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
    [self.view addSubview:_scrollView];

    CGRect f = self.view.bounds;
    f.size.height = kFloatingBarHeight;
    _floatingBarView = [[UIView alloc] initWithFrame:f];
    _floatingBarView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_floatingBarView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _scrollView) {
        CGFloat offsetChange = _lastOffset - scrollView.contentOffset.y;
        CGRect f = _floatingBarView.frame;
        f.origin.y += offsetChange;
        if (f.origin.y < -kFloatingBarHeight) f.origin.y = -kFloatingBarHeight;
        if (f.origin.y > 0) f.origin.y = 0;
        if (scrollView.contentOffset.y <= 0) f.origin.y = 0; //Deal with "bouncing" at the top
        if (scrollView.contentOffset.y + scrollView.bounds.size.height >= scrollView.contentSize.height) f.origin.y = -kFloatingBarHeight; //Deal with "bouncing" at the bottom
        _floatingBarView.frame = f;

        _lastOffset = scrollView.contentOffset.y;
    }
}

你应该这样做 UIView ,而不是 UIViewController 。 iOS开发中的一般规则是视图控制器占据整个屏幕,视图用于占据屏幕一部分的子视图(尽管iPad的情况不是这样)。无论哪种方式, UIViewController 都有它自己的生命周期( willAppear didAppear ,等)浮动条不需要/想要它所以它绝对应该是 UIView

You should be doing it as a UIView, not a UIViewController. The general rule in iOS development is that view controllers take up the entire screen and views are used for "subviews" that take up part of the screen (though this is less the case for iPad). Either way, a UIViewController has it's own lifecycle (willAppear, didAppear, etc.) which is not needed/wanted for the floating bar so it should definitely be a UIView.

这篇关于在Facebook iOS应用程序的时间轴中添加浮动栏到滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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