簡體   English   中英

在UItableView滾動時顯示/隱藏頂視圖和底視圖

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

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

在這里,我實現了UIView的代碼,以便在Tableview滾動時隱藏/顯示。 當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; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM