如何防止UITableView被拉下一定点?

时间:2018-01-20 21:49:57

标签: ios swift uitableview

我已经在UITableView之上实现了一个拉伸标题,当用户拉下桌子时,图像会拉伸,当UITableView被拉起或放开时,图像减少到原来的大小。

这是一个演示:

我有一个UIView容器,其中包含UIImageView,其内容模式设置为 Aspect Fill

下面是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 * 2scrollViewDidScroll,则容器视图会停止拉伸图像。

我想要实现的是,当UITableView被拉下来展开图片时,imageContainerViewHeightConstraint.constant => initialContainerImageViewHeight * 2内的scrollViewDidScroll,我想阻止UITableView不再被拉下来。

目前它看起来像这样:

有没有办法阻止UITableView在满足上述条件时被进一步拉下来,但是仍然允许UITableView被拉回向上

根据 Sh_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 个答案:

答案 0 :(得分:1)

试试这个

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