我想创建一个 UITableView
或 UIScrollView
的子类,当内容偏移量大于 0 时,它会在顶部有一些阴影,表示内容是可滚动的.(见附图)
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)
我现在实现它的方式是使用 UIViewController
,它是 tableView
的委托.我只是在 tableView
顶部有一个 GradientView
,然后截取 scrollViewDidScroll:
以动画化顶部渐变的可见性.
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.
我对这个实现的问题是它不是干净的".我希望我的 UIViewControllers
处理逻辑,而不是处理应用渐变和其他东西.我希望我可以删除一个 UITableView
的子类,它会为我做到这一点.
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.
我面临的挑战是我无法弄清楚 tableView
如何在可滚动内容之上添加一个固定内容.
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.
另一个问题是我应该重写 UIScrollView
的什么方法来拦截滚动事件.显然我不希望 tableView 成为自己的代表......
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...
有什么想法吗?
谢谢!
好的,我在 Apple 的 WWDC 2011 Session 104 video - Advanced Scroll View Techniques 中找到了解决方案.
Ok, so I found the solution on Apple's WWDC 2011 Session 104 video - Advanced Scroll View Techniques.
此视频中有一个完整的部分是关于滚动视图中的固定视图".根据 Apple 的说法,这里的方法是覆盖 layoutSubviews 并将所有代码放在那里以放置您想要的任何位置 - 任何您想要的位置.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!