是否可以添加固定内容到UIScrollView? [英] Is it possible to add fixed content to a UIScrollView?

查看:97
本文介绍了是否可以添加固定内容到UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 UITableView UIScrollView 的子类,当内容的顶部有一些阴影offset是> 0,表示内容是可滚动的。 (见附图)
>



我现在实现的方式是使用 UIViewController ,它是 tableView 。我只需在 tableView 之上有一个 GradientView ,我截取 scrollViewDidScroll:来动画顶层渐变的可见性。



这个实现的问题是它不是干净的。我想让我的 UIViewControllers 来处理逻辑,而不是处理应用渐变和东西。我希望我可以放一个 UITableView 的子类,这将为我做。



我的挑战是我不能弄清楚 tableView 如何在可滚动内容的顶部添加一个固定内容。



另一个问题是什么方法/ s的 UIScrollView 应该覆盖拦截滚动事件。显然我不想让tableView成为自己的委托...



任何想法?



感谢!

解决方案

好的,所以我找到了解决方案在苹果的WWDC 2011 Session 104视频 - 高级滚动视图技术。 >

此视频中有一个整体部分,关于固定视图在滚动视图内。
根据苹果,这里的方式是重写layoutSubviews,并把那里的所有代码放置任何你想要的 - 无论你想要什么。



我试过它和它实际上很容易,它的工作正常。



因此,例如,如果我想在滚动内容时在表的顶部有一个阴影的标题,是代码我应该写:

   - (void)layoutSubviews 
{
[super layoutSubviews]
[self positionTopShadow];
}

- (void)positionTopShadow
{
CGFloat yOffset = self.contentOffset.y;
//我做一些限制,以使阴影视图的最大高度将是40像素
yOffset = MIN(yOffset,40);
yOffset = MAX(0,yOffset);

CGRect frame = self.topShadowView.frame;
//原点应该和内容偏移一样,所以它看起来像
//阴影是在表的顶部(当它实际上只是内容的一部分)
帧.origin = CGPointMake(0,self.contentOffset.y);
frame.size.height = yOffset;
frame.size.width = self.frame.size.width;
self.topShadowView.frame = frame;

if(self.topShadowView.superview == nil)
{
[self addSubview:self.topShadowView];
}
[self bringSubviewToFront:self.topShadowView];
}


I want to create a subclass of UITableView or UIScrollView that will have some shading at the top when the content offset is > 0 to indicate that the content is scrollable. (See image attached)

The way I'm implementing it right now is using the UIViewController that is the delegate of the tableView. I simply have a GradientView on top of the tableView, and I intercept scrollViewDidScroll: to animate the visibility of that top gradient.

My problem with this implementation is that it's not "clean". I want my UIViewControllers to take care of logic, and not to deal with applying gradients and stuff. I wish I could just drop a subclass of UITableView that will do that for me.

The challenge for me is that I can't figure out how the tableView could add to itself a fixed content on top of the scrollable content.

Another question is what method/s of UIScrollView should I override to intercept the scrolling event. Obviously I don't want the tableView to be the delegate of itself...

Any ideas?

Thanks!

解决方案

Ok, so I found the solution on Apple's WWDC 2011 Session 104 video - Advanced Scroll View Techniques.

There is a whole section in this video about "Stationary Views" inside a scroll view. According to Apple, the way to go here is to override layoutSubviews and put there all the code to position whatever you want - wherever you want.

I tried it and it's actually pretty easy and it's working as expected.

So for example if I would like a shadowed header on top of the table when the content is being scrolled, this is the code I should write:

-(void) layoutSubviews
{
    [super layoutSubviews];
    [self positionTopShadow];
}

-(void) positionTopShadow
{
    CGFloat yOffset = self.contentOffset.y;
    // I'm doing some limiting so that the maximum height of the shadow view will be 40 pixels
    yOffset = MIN(yOffset, 40);
    yOffset = MAX(0, yOffset);

    CGRect frame = self.topShadowView.frame;
    // The origin should be exactly like the content offset so it would look like
    // the shadow is at the top of the table (when it's actually just part of the content) 
    frame.origin = CGPointMake(0, self.contentOffset.y);
    frame.size.height = yOffset;
    frame.size.width = self.frame.size.width;
    self.topShadowView.frame = frame;

    if (self.topShadowView.superview == nil)
    {
        [self addSubview:self.topShadowView];
    }
    [self bringSubviewToFront:self.topShadowView];
}

这篇关于是否可以添加固定内容到UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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