【问题标题】:check if UIView is in UIScrollView visible state检查 UIView 是否处于 UIScrollView 可见状态
【发布时间】:2012-06-08 20:28:12
【问题描述】:

检查 UIView 在当前 UIScrollView 的 contentView 上是否可见的最简单和最优雅的方法是什么?有两种方法,一种是涉及UIScrollView的contentOffset.y位置,另一种是转换rect区域?

【问题讨论】:

  • 总结 ????????:RyanG 解决方案适用于部分可见的矩形 --- 并且 --- José 解决方案适用于完全可见的矩形。

标签: iphone objective-c ios ipad


【解决方案1】:

我认为你的想法是正确的。如果是我,我会这样做:

//scrollView is the main scroll view
//mainview is scrollview.superview
//view is the view inside the scroll view

CGRect viewRect = view.frame;
CGRect mainRect = mainView.frame;

if(CGRectIntersectsRect(mainRect, viewRect))
{
    //view is visible
}

【讨论】:

    【解决方案2】:

    在您的滚动视图委托中实现 scrollViewDidScroll: 并手动计算哪些视图可见(例如,通过检查 CGRectIntersectsRect(scrollView.bounds, subview.frame) 是否返回 true。

    【讨论】:

      【解决方案3】:

      如果您要确定视图是否已在屏幕上滚动,请尝试以下操作:

          CGRect thePosition =  myView.frame;
          CGRect container = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.frame.size.width, scrollView.frame.size.height);
          if(CGRectIntersectsRect(thePosition, container))
          {
              // This view has been scrolled on screen
          }
      

      【讨论】:

      • 你可以像这样简化第二行:let container = CGRect(origin: scrollView.contentOffset, size: scrollView.frame.size)
      【解决方案4】:

      为 Swift 3 更新

      var rect1: CGRect!
      // initialize rect1 to the relevant subview
      if rect1.frame.intersects(CGRect(origin: scrollView.contentOffset, size: scrollView.frame.size)) {
              // the view is visible
          }
      

      【讨论】:

        【解决方案5】:

        Swift 5:如果您想触发一个事件来检查整个 UIView 在滚动视图中是否可见:

        extension ViewController: UIScrollViewDelegate {
        
            func scrollViewDidScroll(_ scrollView: UIScrollView) {
                if scrollView.bounds.contains(targetView.frame) {
                    // entire UIView is visible in scroll view
                }
            }
        
        }
        

        【讨论】:

        • 它在将目标视图框架转换为可见的滚动视图框架后工作:let viewFrame = scrollView.convert(targetView.bounds, from: targetView)
        【解决方案6】:

        José 的解决方案不太适合我,它会在我的视图出现在屏幕上之前检测到它。如果 José 的简单解决方案不适合您,以下相交代码在我的 tableview 中完美运行。

            func scrollViewDidScroll(_ scrollView: UIScrollView) {
                let viewFrame = scrollView.convert(targetView.bounds, from: targetView)
                if viewFrame.intersects(scrollView.bounds) {
                    // targetView is visible 
                }
                else {
                    // targetView is not visible
                }
            }
        

        【讨论】:

          【解决方案7】:

          考虑插入的解决方案

          public extension UIScrollView {
              
              /// Returns `adjustedContentInset` on iOS >= 11 and `contentInset` on iOS < 11.
              var fullContentInsets: UIEdgeInsets {
                  if #available(iOS 11.0, *) {
                      return adjustedContentInset
                  } else {
                      return contentInset
                  }
              }
          
              /// Visible content frame. Equal to bounds without insets.
              var visibleContentFrame: CGRect {
                  bounds.inset(by: fullContentInsets)
              }
          }
          
          if scrollView.visibleContentFrame.contains(view) {
              // View is fully visible even if there are overlaying views
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-01-11
            • 2019-12-10
            • 1970-01-01
            • 2020-11-23
            • 2013-04-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多