在scrollView

时间:2016-11-09 14:33:53

标签: ios swift animation uiscrollview

我有UIScrollView里面有一个ImageView。我想要达到的目的是:
- 当用户点击并在用户再次点击时缩小时,放大图像以适合屏幕的整个高度,
- 自动开始滚动到图像右侧的第一个动画,
- 之后,开始滚动到图像中间的第二个动画,
- 最后再次开始动画滚动到图像的右侧 我想实现浮在屏幕上的图像印象。用户不能自己滚动图像。

我已经完成了缩放部分,但我遇到的问题是动画。我尝试过类似的东西,其中offset,middleOffset和endOffset是CGPoints:

override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self
        setupGestureRecognizer()
    }

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        scrollView.panGestureRecognizer.isEnabled = false
        scrollView.pinchGestureRecognizer?.isEnabled = false
    }

fileprivate func updateMinZoomScaleForSize(size: CGSize) {
        let widthScale = size.width / imageView.bounds.width
        let heightScale = size.height / imageView.bounds.height
        let minScale = min(widthScale, heightScale)

        let maxHeightScale = view.frame.height / imageView.bounds.height
        scrollView.maximumZoomScale = maxHeightScale

        scrollView.minimumZoomScale = minScale
        scrollView.zoomScale = minScale
    }

fileprivate func updateConstraintsForSize(size: CGSize) {
        let yOffset = max(0, (size.height - imageView.frame.height) / 2)
        imageViewTopConstraint.constant = yOffset
        imageViewBottomConstraint.constant = yOffset

        let xOffset = max(0, (size.width - imageView.frame.width) / 2)
        imageViewLeadingConstraint.constant = xOffset
        imageViewTrailingConstraint.constant = xOffset

        view.layoutIfNeeded()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        updateMinZoomScaleForSize(size: view.bounds.size)
    }

    func setupGestureRecognizer() {
        let singleTap = UITapGestureRecognizer(target: self, action: #selector(self.handleSingleTap(recognizer:)))
        singleTap.numberOfTapsRequired = 1
        scrollView.addGestureRecognizer(singleTap)
    }

    func handleSingleTap(recognizer: UITapGestureRecognizer) {
        if (scrollView.zoomScale > scrollView.minimumZoomScale) {
            scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
        } else {
            scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
        }
    }

func scrollViewDidScroll(_ scrollView: UIScrollView) {

        UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: { 
            scrollView.setContentOffset(offset, animated: false)
        }, completion: { _ in
            UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: {
                scrollView.setContentOffset(middleOffset, animated: false)
            }, completion: { _ in
                UIView.animate(withDuration: 1, delay: 0.3, options: [], animations: {
                    scrollView.setContentOffset(endOffset, animated: false)
                }, completion: nil)
            })
        })
    }

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    updateConstraintsForSize(size: view.bounds.size)
}

但是它不起作用。第一个动画在图像放大后加载,但随后停止(它不会崩溃)。似乎动画没有完成,因为我再次点击时无法缩小图像。

1 个答案:

答案 0 :(得分:0)

好的,我发现动画块不能在scrollViewDidScroll委托方法中。我将它们放在handleSingleTap函数中,然后它可以正常工作。