簡體   English   中英

如何在iOS中檢測UITextView(UIScrollView)故障的底部

[英]How to detect bottom of UITextView (UIScrollView) glitch in iOS

我正在使用惰性加載器,但遇到一個問題,即在我加載更多數據后, UITextView認為它立即位於視圖的底部,因此錯誤地設置了我的偏移量。

我只是試圖加載新數據,並讓UITextView稍微向下滾動,以便用戶知道已加載了更多數據。

碼:

-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
  float scrollViewHeight = scrollView.frame.size.height;
  float scrollContentSizeHeight = scrollView.contentSize.height;
  float yOffset = scrollView.contentOffset.y;
  NSLog(@"yOffset + scrollViewHeight = %f",yOffset + scrollViewHeight);
  NSLog(@"yOffset = %f",yOffset );

  if (yOffset == 0) {
    // then we are at the top
  }
  else if (yOffset + scrollViewHeight == scrollContentSizeHeight) {
    if (self.updateInProgress) {
      NSLog(@"updateInProgress");
      return;
    }
    // then we are at the end
    self.updateInProgress = TRUE;
    NSLog(@"scrollContentSizeHeight = %f",scrollView.contentSize.height);
    NSLog(@" === at bottom of scrollview ===");

    // JUST NEED TO FIX WHERE SCROLLVIEW APPEARS!
    self.TextView.text = [NSString stringWithFormat:@"%@\n\n%@",self.TextView.text, self.nextChapterString];
    CGPoint offset = self.TextView.contentOffset;
    offset.y = yOffset + scrollViewHeight + 100;
    [self.TextView setContentOffset:offset animated:NO];
    //NSLog(@"updated TextView = %@", self.TextView.text);
    // since disabled scrolling while updating, force scroll down a bit
    //    CGPoint offset = scrollView.contentOffset;
    //    offset.y += 100;
    //    [scrollView setContentOffset:offset animated:YES];

  }
  self.updateInProgress = FALSE;
}

OUTPUT:

這是我看到的輸出,如您所見,在加載更多數據后,我的yOffset發生了巨大變化。

2014-05-07 23:11:40.370 Test[6314:60b] yOffset + scrollViewHeight = 1821.500000
2014-05-07 23:11:40.370 Test[6314:60b] yOffset = 1432.500000
2014-05-07 23:11:40.371 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.403 Test[6314:60b] yOffset + scrollViewHeight = 1821.000000
2014-05-07 23:11:40.403 Test[6314:60b] yOffset = 1432.000000
2014-05-07 23:11:40.404 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.470 Test[6314:60b] yOffset + scrollViewHeight = 1820.500000
2014-05-07 23:11:40.471 Test[6314:60b] yOffset = 1431.500000
2014-05-07 23:11:40.471 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.504 Test[6314:60b] yOffset + scrollViewHeight = 1820.000000
2014-05-07 23:11:40.505 Test[6314:60b] yOffset = 1431.000000
2014-05-07 23:11:40.505 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.506 Test[6314:60b] scrollContentSizeHeight = 1820.000000
2014-05-07 23:11:40.506 Test[6314:60b]  === at bottom of scrollview ===
2014-05-07 23:11:40.508 Test[6314:60b] yOffset + scrollViewHeight = 2309.000000
2014-05-07 23:11:40.508 Test[6314:60b] yOffset = 1920.000000
2014-05-07 23:11:40.508 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.509 Test[6314:60b] yOffset + scrollViewHeight = 1954.000000
2014-05-07 23:11:40.509 Test[6314:60b] yOffset = 1565.000000
2014-05-07 23:11:40.510 Test[6314:60b] yOffset = 389.000000
2014-05-07 23:11:40.510 Test[6314:60b] scrollContentSizeHeight = 1954.000000
2014-05-07 23:11:40.510 Test[6314:60b]  === at bottom of scrollview ===
2014-05-07 23:11:40.512 Test[6314:60b] yOffset + scrollViewHeight = 2443.000000
2014-05-07 23:11:40.512 Test[6314:60b] yOffset = 2054.000000
2014-05-07 23:11:40.513 Test[6314:60b] yOffset = 389.000000

關於我在做什么錯的任何建議嗎?

嘗試這個

    text.text = [NSString stringWithFormat:@"%@\n\n%@",text.text, str];
    CGPoint offset = text.contentOffset;
    offset.y = yOffset + scrollViewHeight + 100;
    text.delegate = nil;//set your delegate nil
    [text setContentOffset:offset animated:NO];
    text.delegate = self;//set back

暫無
暫無

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

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