使用UITapGestureRecognizer

时间:2016-11-14 11:31:47

标签: ios swift uiscrollview uiviewanimation uitapgesturerecognizer

我在我的应用中破坏动画块时出现此问题。我正在为UIScrollView内的图像制作动画(放大,从左向右移动,缩小)。正在使用UITapGestureRecognizer启动动画。

问题在于,即使我可以在动画制作时缩小图像,动画也会继续阻止,因此我会将图像从iPhone屏幕上移开。

我尝试在removeAllAnimations()scrollView.layer上使用view.layer方法,但似乎无效。

这是我的代码:

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

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

    func handleSingleTap(recognizer: UITapGestureRecognizer) {
        animateRecipeImage()
    }


    // MARK: - Recipe Image Animation

    func animateRecipeImage() {
        let offset = CGPoint(x: 0, y: 0)
        var middleOffset = CGPoint()
        let endOffset = CGPoint(x: (imageView.bounds.width / 2), y: 0)

        // To avoid animation in landscape mode we check for current device orientation

        if (UIDeviceOrientationIsPortrait(UIDevice.current.orientation)) {
            if (scrollView.zoomScale > scrollView.minimumZoomScale) {
                scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)

                // Remove all animations and update image contraints to fit the screen
                self.scrollView.layer.removeAllAnimations()
                self.view.layer.removeAllAnimations()
                updateConstraintsForSize(size: view.bounds.size)

            } else {
                scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
                let screenWidth = screenSize.width

                // Handle big screen sizes
                if (screenWidth < self.imageView.bounds.width) {
                    middleOffset = CGPoint(x: ((self.imageView.bounds.width - screenWidth) * self.scrollView.zoomScale), y: 0)
                } else {
                    middleOffset = CGPoint(x: (self.imageView.frame.width - screenWidth), y: 0)
                }

                // Start animating the image
                UIView.animate(withDuration: 2, delay: 0.5, options: [.allowUserInteraction], animations: {
                    self.scrollView.setContentOffset(offset, animated: false)
                }, completion: { _ in
                    UIView.animate(withDuration: 4, delay: 0.5, options: [.allowUserInteraction], animations: {
                        self.scrollView.setContentOffset(middleOffset, animated: false)
                    }, completion: { _ in
                        UIView.animate(withDuration: 1.0, delay: 0.2, usingSpringWithDamping: 0.4, initialSpringVelocity: 1.1, options: [.allowUserInteraction], animations: {
                            self.scrollView.setContentOffset(endOffset, animated: false)
                        }, completion: nil)
                    })
                })
            }
        }
    }

任何想法如何打破动画块?我想要实现的结果是打破动画和缩小图像。

0 个答案:

没有答案