在UItableView滚动时显示/隐藏顶视图和底视图

时间:2015-08-14 13:49:03

标签: ios objective-c uitableview

我在UITableView滚动时隐藏了顶视图和底视图(UIViews)。现在,我需要检查用户是否开始再次向上拖动UITableview并返回初始位置的uiviews。我有以下代码来完成第一步:在滚动uitableview时隐藏/显示

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(!self.isScrollingFast) {

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

NSInteger yOffset = scrollView.contentOffset.y;
if (yOffset > 0) { 
        self.tabBar.frame = CGRectMake(self.tabBar.frame.origin.x, self.originalFrame.origin.y + yOffset, self.tabBar.frame.size.width, self.tabBar.frame.size.height);

       self.viewTopo.frame = CGRectMake(self.viewTopo.frame.origin.x, self.originalFrameTopo.origin.y - yOffset, self.viewTopo.frame.size.width, self.viewTopo.frame.size.height); 


    if(self.originalFrameHidingView.origin.y - yOffset >= 0) {
        self.hidingView.frame = CGRectMake(self.hidingView.frame.origin.x, self.originalFrameHidingView.origin.y - yOffset, self.hidingView.frame.size.width, self.hidingView.frame.size.height); 
    }
    else {
        self.hidingView.frame = CGRectMake(self.hidingView.frame.origin.x, -10, self.hidingView.frame.size.width, self.hidingView.frame.size.height); 
    }

    [self.tbPertos setFrame:CGRectMake(self.tbPertos.frame.origin.x, self.hidingView.frame.origin.y + self.hidingView.frame.size.height, self.tbPertos.frame.size.width, self.tbPertos.frame.size.height)];

    if(self.tbPertos.frame.size.height + self.tbPertos.frame.origin.y + yOffset <= screenHeight)
        self.tbPertos.frame = CGRectMake(self.tbPertos.frame.origin.x, self.tbPertos.frame.origin.y, self.tbPertos.frame.size.width, self.tbPertos.frame.size
                                         .height+yOffset);
    else {  
        self.tbPertos.frame = CGRectMake(self.tbPertos.frame.origin.x, self.tbPertos.frame.origin.y, self.tbPertos.frame.size.width, screenHeight - self.tbPertos.frame.origin.y);
    }

}
if (yOffset < 1) {
    self.tabBar.frame = self.originalFrame;
    self.viewTopo.frame = self.originalFrameTopo;
    self.hidingView.frame = self.originalFrameHidingView;
    self.tbPertos.frame = CGRectMake(self.tbPertos.frame.origin.x, self.hidingView.frame.origin.y + self.hidingView.frame.size.height, self.tbPertos.frame.size.width, screenHeight - self.tbPertos.frame.origin.y);
     }
   }
 }

当用户开始向上滚动时,我正在尝试执行顶部和底部视图的代码重新出现。独立地滚动偏移。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    CGPoint currentOffset = scrollView.contentOffset;
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];

    NSTimeInterval timeDiff = currentTime - self.lastOffsetCapture;
            CGFloat distance = currentOffset.y - self.lastOffset.y;
        //The multiply by 10, / 1000 isn't really necessary.......

        if (distance < 0) {
            if(!self.isScrollingFast) {
                NSLog(@"voltar posicao normal");


                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.5];
                [UIView setAnimationDelay:1.0];
                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];


                self.tabBar.frame = self.originalFrame;
                self.viewTopo.frame = self.originalFrameTopo;
                self.hidingView.frame = self.originalFrameHidingView;
                self.tbPertos.frame = self.originalFrameTbPertos;
                self.isScrollingFast = YES;

                [UIView commitAnimations];


            }
        } else {
            self.isScrollingFast = NO;
        }

        self.lastOffset = currentOffset;
        self.lastOffsetCapture = currentTime;
}

1 个答案:

答案 0 :(得分:2)

这里我在桌面滚动时为UIView隐藏/显示实现了代码。当tableview向下滚动时,UIView被隐藏,当向上滚动时,UIView显示。我希望它为你工作......!

步骤1: - 在.h文件中创建一个属性

@property (nonatomic) CGFloat previousContentOffset;

步骤2: - 在scrollViewDidScroll方法中记下此代码。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat currentContentOffset = scrollView.contentOffset.y;

if (currentContentOffset > self.previousContentOffset) {
    // scrolling towards the bottom
    [self.subButtonView setHidden:YES];
} else if (currentContentOffset < self.previousContentOffset) {
    // scrolling towards the top
    [self.subButtonView setHidden:NO];
}
self.previousContentOffset = currentContentOffset; 
}