代码之家  ›  专栏  ›  技术社区  ›  Pangu

如何防止UITableView被拉过某个点?

  •  0
  • Pangu  · 技术社区  · 7 年前

    我在 UITableView 其中,当用户拉下桌子时,图像被拉伸,当 UITableView 拉起或放开时,图像将缩小回其原始大小。

    下面是一个演示:

    我有一个 UIView 包含 UIImageView 将其内容模式设置为 方面填充 .

    下面是一个 UITableView 如演示中所示。

    我使用 UITableView's scrollView委托方法,以确定何时拉伸和缩小容器视图,如下所示:

    extension MyViewController: UIScrollViewDelegate
    {
        func scrollViewDidScroll(_ scrollView: UIScrollView)
        {
            if scrollView.contentOffset.y < 0 &&
                imageContainerViewHeightConstraint.constant < initialContainerImageViewHeight * 2
            {
                imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
            }
            else if scrollView.contentOffset.y > 0 &&
                imageContainerViewHeightConstraint.constant > initialContainerImageViewHeight
            {
                imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
            }
        }
    
        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
        {
            resetContainerViewSize()
        }
    
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
        {
            resetContainerViewSize()
        }
    }
    
    func resetContainerViewSize()
    {
        imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight
    
        UIView.animate(withDuration: 0.4,
                           delay: 0.0,
                           usingSpringWithDamping: 0.7,
                           initialSpringVelocity: 0.5,
                           options: .curveEaseInOut,
                           animations: {
                            self.view.layoutIfNeeded()
                        }, completion: nil)
    }
    

    如果 scrollView.contentOffset.y < 0 imageContainerViewHeightConstraint.constant => initialContainerImageViewHeight * 2 在里面 scrollViewDidScroll ,容器视图停止拉伸图像。

    我想实现的是 UITableView 正在下拉以展开图像,一次 ImageContainerViewWightConstraint。常量=>initialContainerImageViewHeight*2 在内部 滚动视图DidScroll ,我想防止 UITableView 不会再被拉下。

    目前看起来是这样的:

    有没有办法防止 UITableView 当满足上述条件时,不会被进一步拉下,但是 允许 UITableView 待拉 支持 ?

    根据建议 Sh\u Khan:

    extension MyViewController: UIScrollViewDelegate
    {
        func scrollViewDidScroll(_ scrollView: UIScrollView)
        {
            if scrollView.contentOffset.y < 0 &&
                imageContainerViewHeightConstraint.constant < initialContainerImageViewHeight * 2
            {
                imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
            }
            else if scrollView.contentOffset.y < 0 && imageContainerViewHeightConstraint.constant >= initialContainerImageViewHeight * 2
            {
                imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight * 2
    
                view.layoutIfNeeded()
    
                dataTableView.frame = CGRect.init(x: 0.0,
                                              y: initialContainerImageViewHeight * 2,
                                              width: dataTableView.frame.size.width,
                                              height:     dataTableView.frame.size.height)
    
                return
            }
            else if scrollView.contentOffset.y > 0 &&
                imageContainerViewHeightConstraint.constant > initialContainerImageViewHeight
            {
                imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
            }
        }
    
        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
        {
            resetContainerViewSize()
        }
    
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
        {
            resetContainerViewSize()
        }
    }
    
    func resetContainerViewSize()
    {
        imageContainerViewHeightConstraint.constant = initialContainerImageViewHeight
    
        UIView.animate(withDuration: 0.4,
                           delay: 0.0,
                           usingSpringWithDamping: 0.7,
                           initialSpringVelocity: 0.5,
                           options: .curveEaseInOut,
                           animations: {
                            self.view.layoutIfNeeded()
                            self.dataTableView.frame = CGRect.init(x: 0.0,
                                                                   y: self.initialContainerImageViewHeight,
                                                                   width: self.dataTableView.frame.size.width,
                                                                   height: self.dataTableView.frame.size.height)
                        }, completion: nil)
    }
    

    结果是:

    1 回复  |  直到 7 年前
        1
  •  1
  •   Shehata Gamal    7 年前

    试试这个

    extension ViewController: UIScrollViewDelegate
    {
       func scrollViewDidScroll(_ scrollView: UIScrollView)
       {
    
        print("ddffddfd \(scrollView.contentOffset.y)")
    
        if scrollView.contentOffset.y > 0 && imageContainerViewHeightConstraint.constant == 223
        {
            return
        }
    
        if scrollView.contentOffset.y > 0
        {
    
            var sd = imageContainerViewHeightConstraint.constant + abs(scrollView.contentOffset.y)
    
    
            if(sd < 233 )
            {
    
                print("path111    1")
    
                  self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0  )
    
                return
    
    
            }
            else
            {
                 print("path111    2")
                 imageContainerViewHeightConstraint.constant -= abs(scrollView.contentOffset.y)
            }
    
             print("path11111    3")
            view.layoutIfNeeded()
    
             self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0  )
            return
        }
    
    
         print("path11111   4")
    
    
    
    
        if scrollView.contentOffset.y < 0 && imageContainerViewHeightConstraint.constant >= initialContainerImageViewHeight * 2
        {
            self.dataTableView.contentOffset = CGPoint.init(x: 0, y: 0  )
    
            //self.dataTableView.bounces = false
    
            return
        }
        else
        {
            imageContainerViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
    
             view.layoutIfNeeded()
    
        }
    
       }
    
        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
        {
            resetContainerViewSize()
        }
    
        func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
        {
            resetContainerViewSize()
        }
    }
    

    ///////

    func resetContainerViewSize()
    {
        imageContainerViewHeightConstraint.constant = 223
    
        UIView.animate(withDuration: 0.7,
                       delay: 0.0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0.5,
                       options: .curveEaseInOut,
                       animations: {
                        self.view.layoutIfNeeded()   
                        self.dataTableView.bounces = true
                    }, completion: nil)
    }
    

    正在运行

    enter image description here

    在此处查找演示 testScrollAboveTable