我正试图产生同样的效果,因为Instagram在标题中有。我怎么能这样做?
我尝试了很多解决方案。
最好 - https://github.com/andreamazz/AMScrollingNavbar
但它有一个大问题 - 它正在使用uipangesturerecognizer移动吧。这对我来说很糟糕,因为如果桌子位于顶部,我想要显示条形图。
我试图改变这个控件的工作来滚动视图委托,但是发现了很多问题,有什么想法,他们是怎么做到的?
答案 0 :(得分:3)
我找到了解决方案,就像你说的那样 - 你必须稍微处理滚动视图委托,但是经过大约2个小时后我才知道这一切。我试图解决的问题是能够像在Instagram中那样在一个连续的动作中获得标题。
因此,首先检查xib设置,它有一个标题视图(0 20,320 85),它位于(0 20,320 548)表视图的正后方
所以这是启动后的样子(黄色的表视图框):
这是我想要它在拉下后的样子(标题框架为红色):
所以我只是粘贴带注释的代码,我希望它足够容易理解。
进行定义
#define SIGNIFICANT_SCROLLING_DISTANCE 200
创建两个属性
@property (nonatomic) CGFloat lastScrollViewOffsetY;
@property (nonatomic) CGFloat distancePulledDownwards;
为scrollViewDidScroll
委托方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// store current scroll view frame as we will change it later on and set it
// back to the scroll view in the very end
CGRect currentScrollViewRect = scrollView.frame;
// same with the content offset
CGPoint currentScrollViewOffset = scrollView.contentOffset;
CGFloat offsetShiftY = self.lastScrollViewOffsetY - scrollView.contentOffset.y;
if (offsetShiftY > 0) {
// pulling downwards
// keep trrack of the distance that we pulled downwards
self.distancePulledDownwards += offsetShiftY;
// header opens (table view shifts its frame down) in two cases:
// 1. contentOffset.y<0
// 2. scrolled downwards a significant amount or header is already open
// but in both cases we have to make sure that it doesn't open further than we want it to
CGFloat wantedOriginY = currentScrollViewRect.origin.y;
if ((scrollView.contentOffset.y<0) || (self.distancePulledDownwards > SIGNIFICANT_SCROLLING_DISTANCE) || (currentScrollViewRect.origin.y>20)){
// shift scroll views frame by offset shift
wantedOriginY = currentScrollViewRect.origin.y + offsetShiftY;
// compensate that shift by moving content offset back
currentScrollViewOffset.y += (wantedOriginY <= 105) ? offsetShiftY : 0;
}
currentScrollViewRect.origin.y = (wantedOriginY <= 105) ? wantedOriginY : 105;
}
else {
// pulling upwards
self.distancePulledDownwards = 0;
// header closes (table view shifts its frame up) in one case: when it is open =) (and contentOffset.y>0 to eliminate closing on bounce)
if (scrollView.contentOffset.y > 0) {
CGFloat wantedOriginY = currentScrollViewRect.origin.y + offsetShiftY;
currentScrollViewRect.origin.y = (wantedOriginY >= 20) ? wantedOriginY : 20;
currentScrollViewOffset.y += (wantedOriginY >= 20) ? offsetShiftY : 0;
}
}
// set the changed (if it was changed at all) frame to the scroll view
[scrollView setFrame:currentScrollViewRect];
// correct offset using a special trick
// it ensures that scrollViewDidScroll: won't be called on setting the offset
scrollView.delegate = nil;
[scrollView setContentOffset:currentScrollViewOffset];
scrollView.delegate = self;
// and finally remember the current offset as the last
self.lastScrollViewOffsetY = scrollView.contentOffset.y;
}
享受光滑的桌面在屏幕上来回滚动=) 这也可以修改,您可以添加和调整标题大小,使其基本上与Instagram相同。
答案 1 :(得分:2)
将AMScrollingNavbar与tableViewController一起使用,并覆盖willDisplayCell
方法以显示表格是否滚动到顶部:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
[self showNavbar];
}
}
答案 2 :(得分:2)
您可以在班级中使用下面提到的方法,在Instagram中为导航栏添加效果。
- (void)scrollViewDidScroll:(UIScrollView *)sender {
//Initializing the views and the new frame sizes.
UINavigationBar *navbar =self.navigationController.navigationBar;
UIView *tableView = self.view;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect tableFrame = self.view.frame;
//changing the origin.y based on the current scroll view.
//Adding +20 for the Status Bar since the offset is tied into that.
if (isiOS7) {
navBarFrame.origin.y = MIN(0, MAX(-44, (sender.contentOffset.y * -1))) +20 ;
tableFrame.origin.y = navBarFrame.origin.y + navBarFrame.size.height;
}else{
navBarFrame.origin.y = MIN(0, (sender.contentOffset.y * -1)) +20;
tableFrame.origin.y = MIN(0,MAX(-44,(sender.contentOffset.y * -1))) ;
}
navbar.frame = navBarFrame;
tableView.frame = tableFrame;
}
答案 3 :(得分:1)
使用该库,将其添加到视图控制器:
- (BOOL)scrollViewShouldScrollToTop {
[self showNavbar];
return YES;
}