上下滚动时增加或减少imageView的高度

时间:2019-05-24 07:11:20

标签: swift iphone scrollview

上下滚动时增加或减小imageView的高度

extension DetailViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {


            if (self.lastContentOffset > scrollView.contentOffset.y) {
                UIView.animate(withDuration: 0.2, animations: {
                    print("going down")
                    if self.imgViewHeight.constant >= 200 {
                        self.blurView.alpha = 0

                        self.backButton.isHidden = false
                    }
                    else if self.imgViewHeight.constant <= 200{
                        self.blurView.alpha = 1


                        self.imgViewHeight.constant = self.imgViewHeight.constant + 4

                    }
                }, completion: nil)

            }
            else if (self.lastContentOffset < scrollView.contentOffset.y) {

                UIView.animate(withDuration: 0.2, animations: {
                    if self.imgViewHeight.constant <= 72 {
                       self.blurView.alpha = 1
                        self.backButton.isHidden = false
                        self.descriptions.isScrollEnabled = true
                    }
                    else if self.imgViewHeight.constant >= 72{
                        self.blurView.alpha = 1

                        self.imgViewHeight.constant = self.imgViewHeight.constant - 4

                    }

                    print("lolol:\(self.imgViewHeight.constant)")

                }, completion: nil)

            }
            else {

            }

            self.lastContentOffset = scrollView.contentOffset.y
        }
}

3 个答案:

答案 0 :(得分:0)

您可以使用ScrollView函数,例如:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
//your code when you want start scrolling

}

func scrollViewWillEndDragging(_ scrollView: UIScrollView) {
//your code when you want run after stop scrolling

}

答案 1 :(得分:0)

设置layoutIfNeeded()的值后,您需要在view UIView.animate(withDuration:animations:)的{​​{1}}上调用 animations closure

imgViewHeight.constant

在上面的代码中,我使用了extension DetailViewController: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { if (self.lastContentOffset > scrollView.contentOffset.y) { UIView.animate(withDuration: 0.2) {[weak self] in self?.imgViewHeight.constant = 20 //This is just an example self?.view.layoutIfNeeded() //Here.......... } } else if (self.lastContentOffset < scrollView.contentOffset.y) { UIView.animate(withDuration: 0.2) {[weak self] in self?.imgViewHeight.constant = 0 //This is just an example self?.view.layoutIfNeeded() //Here.......... } } //Rest of the code... } } 的样本值。在那里添加您自己的height constraint (20 and 0)逻辑。

答案 2 :(得分:0)

您好,感谢您的帮助和答复。我用了MGCollapsingHeader吊舱