問題描述
我想創建一個 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模板網!