简体   繁体   English

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

[英]Show/Hide Top View and Bottom View while UItableView Scrolling

I did my Top View and Bottom View (UIViews) hide while the UITableView is scrolling. 当UITableView滚动时,我隐藏了俯视图和仰视图(UIViews)。 Now, I need to check if the user begin drag the UITableview to up again and back the uiviews for the initial position. 现在,我需要检查用户是否开始将UITableview再次向上拖动,然后将uiviews返回初始位置。 I have the following code to do the first step: hidden/show while scrolling uitableview 我具有以下代码来执行第一步:滚动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);
     }
   }
 }

And there's the code which I'm trying to do the Top and Bottom View reappear when the user begin scroll up. 当用户开始向上滚动时,有一些代码正在尝试重新显示“顶视图”和“底视图”。 Independently wheres the scroll offset. 独立地滚动偏移量。

-(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;
}

Here i implemented code for UIView Hide / Show when tableview scrolling. 在这里,我实现了UIView的代码,以便在Tableview滚动时隐藏/显示。 When tableview scrolling down then UIView is hidden and when scrolling up then UIView show. 当tableview向下滚动时,UIView被隐藏,而向上滚动则UIView显示。 I hope it's working for you...! 希望它对您有用...!

Step 1:- Make one property in .h file 步骤1:-在.h文件中创建一个属性

@property (nonatomic) CGFloat previousContentOffset;

Step 2:- Write down this code in scrollViewDidScroll Method. 步骤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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM