0
votes

I did my Top View and Bottom View (UIViews) hide while the UITableView is scrolling. Now, I need to check if the user begin drag the UITableview to up again and back the uiviews for the initial position. I have the following code to do the first step: hidden/show while scrolling 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;
}
1
Whats happen when you debug with break points?tuledev
@anhtu simply doesn't work how I expect. Forget the second step I posted. The first step works great....When the user is scrolling the views hide...Now, how could I do for show again the views(with animation effects) when user begin scroll to up again?ladessa-ios

1 Answers

2
votes

Here i implemented code for UIView Hide / Show when tableview scrolling. When tableview scrolling down then UIView is hidden and when scrolling up then UIView show. I hope it's working for you...!

Step 1:- Make one property in .h file

@property (nonatomic) CGFloat previousContentOffset;

Step 2:- Write down this code in scrollViewDidScroll Method.

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