【问题标题】:Show/Hide Top View and Bottom View while UItableView ScrollingUItableView 滚动时显示/隐藏顶视图和底视图
【发布时间】:2015-08-14 13:49:03
【问题描述】:

我在 UITableView 滚动时隐藏了我的顶视图和底视图 (UIViews)。现在,我需要检查用户是否开始将 UITableview 再次向上拖动并返回初始位置的 uiview。我有以下代码来做第一步:滚动 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;
}

【问题讨论】:

  • 使用断点调试会发生什么?
  • @anhtu 根本无法按我的预期工作。忘记我发布的第二步。第一步效果很好......当用户滚动视图时隐藏......现在,当用户再次开始向上滚动时,我该如何再次显示视图(带有动画效果)?

标签: ios objective-c uitableview


【解决方案1】:

这里我实现了表格视图滚动时 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; 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2012-08-09
    • 2011-11-19
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多