最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中检测列表滚动到顶部

iOS crifan 2598浏览 0评论

swift when tableview scroll to top

swift when  scroll to top

swift detect  scroll to top

ios – Detecting the bottom "bounce" of UITableView – Stack Overflow

ios – Detect UIScrollview bottom reached – Stack Overflow

iphone – iOS : detect UIWebView reaching the top or bottom – Stack Overflow

iphone – UIScrollView, reaching the bottom of the scroll view – Stack Overflow

[整理]swift中列表中的scrollViewDidScrollToTop的作用和用法

但是也还是自己去试试:

我向上滚动到顶部后,执行顺序是:

1.两次scrollViewDidScroll

<code>   func scrollViewDidScroll(scrollView: UIScrollView) {
        gLog.debug(&quot;scrollView=\(scrollView)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -1.5}; contentSize: {320, 1578}&gt;
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;
    }
</code>

2.一次scrollViewWillEndDragging

<code>    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer&lt;CGPoint&gt;) {
        gLog.debug(&quot;scrollView=\(scrollView), velocity=\(velocity), targetContentOffset=\(targetContentOffset)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;, velocity=(0.0, -0.26938), targetContentOffset=0xbff08178
    }
</code>

3.一次scrollViewDidEndDragging

<code>    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        gLog.debug(&quot;scrollView=\(scrollView), decelerate=\(decelerate)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;, decelerate=true
    }
</code>

4.一次scrollViewWillBeginDecelerating

<code>    func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
        gLog.debug(&quot;scrollView=\(scrollView)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;
    }
</code>

5.再次scrollViewDidScroll

<code>    func scrollViewDidScroll(scrollView: UIScrollView) {
        gLog.debug(&quot;scrollView=\(scrollView)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -1.5}; contentSize: {320, 1578}&gt;
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, 0}; contentSize: {320, 1578}&gt;
    }
</code>

6.一次scrollViewDidEndDecelerating

<code>    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        gLog.debug(&quot;scrollView=\(scrollView)&quot;)
        //scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -0}; contentSize: {320, 1578}&gt;
    }
</code>

完整log如下:

<code>2016-04-16 10:17:32.356 [Debug] [main] [MessageTableViewController.swift:910] scrollViewDidScroll &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -1.5}; contentSize: {320, 1578}&gt;
2016-04-16 10:18:10.804 [Debug] [main] [MessageTableViewController.swift:910] scrollViewDidScroll &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;
2016-04-16 10:18:22.657 [Debug] [main] [MessageTableViewController.swift:906] scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;, velocity=(0.0, -0.26938), targetContentOffset=0xbff08178
2016-04-16 10:18:54.149 [Debug] [main] [MessageTableViewController.swift:898] scrollViewDidEndDragging(_:willDecelerate:) &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;, decelerate=true
2016-04-16 10:20:20.922 [Debug] [main] [MessageTableViewController.swift:886] scrollViewWillBeginDecelerating &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -10}; contentSize: {320, 1578}&gt;
2016-04-16 10:20:58.607 [Debug] [main] [MessageTableViewController.swift:910] scrollViewDidScroll &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, 0}; contentSize: {320, 1578}&gt;
2016-04-16 10:21:33.192 [Debug] [main] [MessageTableViewController.swift:882] scrollViewDidEndDecelerating &gt; scrollView=&lt;UITableView: 0x7c338c00; frame = (0 0; 320 460); clipsToBounds = YES; autoresize = H+BM; gestureRecognizers = &lt;NSArray: 0x7df70a80&gt;; layer = &lt;CALayer: 0x7df4fb00&gt;; contentOffset: {0, -0}; contentSize: {320, 1578}&gt;
</code>

而别人的那个办法:

scrollViewDidScroll中判断scrollView.contentOffset.y小于0

虽然可以判断移动到顶部了

-》但是后执行多次

所以还是用那个:

scrollView.contentOffset.y == 0

去判断是否滚动到顶部了。

转载请注明:在路上 » [已解决]swift中检测列表滚动到顶部

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.244 seconds, using 22.18MB memory