Iphone UITableView节标题在';浮动';?
在iOS设备上,UITableView中的节标题有一个很好的行为,当您滚动一个节时,它们会粘贴或“浮动”到屏幕顶部。在我的特殊情况下,节头是从其他XIB文件加载的 是否可以根据节标题当前是否处于浮动状态来更改节标题?具体来说,我想添加一个小阴影,使其仅在粘贴到视图顶部时显示在标题下Iphone UITableView节标题在';浮动';?,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,在iOS设备上,UITableView中的节标题有一个很好的行为,当您滚动一个节时,它们会粘贴或“浮动”到屏幕顶部。在我的特殊情况下,节头是从其他XIB文件加载的 是否可以根据节标题当前是否处于浮动状态来更改节标题?具体来说,我想添加一个小阴影,使其仅在粘贴到视图顶部时显示在标题下 谢谢 我还没有测试过它,但我看不出有什么理由不可能。 只需确保设置了正确的边界(因为阴影需要位于视图的顶部,而不是上方) 您可以使用以下方法: 使用scrollView:didScroll:获取有关滚动事件的通知 在
谢谢 我还没有测试过它,但我看不出有什么理由不可能。 只需确保设置了正确的
边界
(因为阴影需要位于视图的顶部,而不是上方)
您可以使用以下方法:
scrollView:didScroll:
获取有关滚动事件的通知[view addSubview:shadowView]
)类似于CGRectMake(0.f,您的默认HeaderHeight,320.f,您的shadowHeight)
应该是阴影视图的框架
视图的边界
,这样它就可以显示你的阴影视图
:CGRectMake(0.f,0.f-yourShadowHeight,320.f,yoursdefaultheaderheight+2*yourShadowHeight)
scrollView:didcoll:
),请删除阴影视图你的
headerView
sbounds
应该是0.f-你的阴影高度
,因为如果你只使用0.f
,它会模糊(我不知道为什么…。你必须在标题中有自己的UIView。那么你需要一个参考资料。然后用您的UIScrollViewDelegate钩住ScrollViewWillBeginDraging:
。在该函数中,将阴影添加到自定义视图中
挂接到ScrollViewDiEndDraging:将减速:并删除此函数中的阴影。以下是我创建的函数,用于更新每个标题是否有阴影。本例中的所有节标题都是UIView子类
ListHeader
。它们由viewForHeaderInSection
函数保留和返回
- (void) updateHeaderShadows {
int i=0;
int sectionHeight = 0;
int totalHeight = 0;
UIView * sectionHeader;
while (i<[self numberOfSectionsInTableView:self.tableView]) {
sectionHeight = [self.tableView rectForSection:i].size.height;
sectionHeader = [self tableView:self.tableView viewForHeaderInSection:i];
if ([sectionHeader respondsToSelector:@selector(shadow)]) {
if (sectionHeader.frame.origin.y == totalHeight || sectionHeader.frame.origin.y == totalHeight + sectionHeight - sectionHeader.frame.size.height) {
[((ListHeader *) sectionHeader).shadow setHidden:YES];
} else {
[((ListHeader *) sectionHeader).shadow setHidden:NO];
}
}
totalHeight += sectionHeight;
i++;
}
}
—(无效)updateHeaderShadows{
int i=0;
int sectionHeight=0;
int totalHeight=0;
UIView*节标题;
而(我@Anthony Mattox则回答了Swift的问题
protocol SectionHeaderWithShadowProtocol where Self: UIView {
var shadow: Bool { get set }
}
class SectionHeaderView: UITableViewHeaderFooterView, SectionHeaderWithShadowProtocol {
@IBOutlet weak var shadowView: UIView!
var shadow: Bool = false {
didSet {
shadowView.isHidden = shadow
}
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateHeaderShadows()
}
func updateHeaderShadows() {
var i = 0
var sectionHeight: CGFloat = 0
var totalHeight: CGFloat = 0
while i < numberOfSections() {
sectionHeight = tableView.rect(forSection: i).size.height
if let sectionHeader = tableView.headerView(forSection: i) as? SectionHeaderWithShadowProtocol {
if sectionHeader.frame.origin.y == totalHeight || sectionHeader.frame.origin.y == totalHeight + sectionHeight - sectionHeader.frame.size.height {
sectionHeader.shadow = false
} else {
sectionHeader.shadow = true
}
}
totalHeight += sectionHeight
i += 1
}
}
protocol SectionHeaderWithShadowProtocol其中Self:UIView{
var shadow:Bool{get set}
}
类SectionHeaderView:UITableViewHeaderFooterView,SectionHeaderWithShadowProtocol{
@ibvar shadowView:UIView!
var shadow:Bool=false{
迪塞特{
shadowView.ishiden=阴影
}
}
}
func scrollViewDidScroll(scrollView:UIScrollView){
updateHeaderShadows()
}
func updateHeaderShadows(){
变量i=0
变量截面高度:CGFloat=0
var totalHeight:CGFloat=0
而我{
截面高度=tableView.rect(对于截面:i).size.height
如果让sectionHeader=tableView.headerView(对于节:i)作为?SectionHeaderWithShadowProtocol{
如果sectionHeader.frame.origin.y==总高度| | sectionHeader.frame.origin.y==总高度+截面高度-sectionHeader.frame.size.height{
sectionHeader.shadow=false
}否则{
sectionHeader.shadow=true
}
}
总高度+=截面高度
i+=1
}
}
谢谢Tim。你能在步骤2上展开一点吗?你如何确定任何给定的标题视图是否是浮动的?BOOL isFloating=(headerView.frame.origin.y+scrollView.contentOffset.y==0);
=)!(我不确定contentOffset.y
是正数还是负数,因此如果它不起作用,请将+
替换为-
:)!由于某些原因,当标题浮动时,标题原点和内容偏移量没有完全对齐,并且添加了一个小的摆动空间,使阴影忽隐忽现。感谢您为我指出了正确的方向!我得到了一些类似的结果。我会在下面尽可能发布它。