Pan Gesture动画

时间:2017-06-18 18:30:04

标签: ios swift animation uiscrollview uipangesturerecognizer

我希望使用平移手势实现类似动画的滚动视图。 我已经使用pan手势操作方法下面的代码实现了scrollview的几乎所有属性:

if recognizer.state == UIGestureRecognizerState.changed{

        recognizer.view!.center = CGPoint(x: recognizer.view!.center.x, y: recognizer.view!.center.y + translation.y )

        recognizer.setTranslation(CGPoint.zero, in: self.view)

    }

    if recognizer.state == UIGestureRecognizerState.ended {
        // 1
        let velocity = recognizer.velocity(in: self.view)
        let magnitude = sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))
        let slideMultiplier = magnitude / 200
        print("magnitude: \(magnitude), slideMultiplier: \(slideMultiplier)")

        // 2
        let slideFactor = 0.1 * slideMultiplier     //Increase for more of a slide
        // 3
        var finalPoint = CGPoint(x:recognizer.view!.center.x ,
                                 y:recognizer.view!.center.y + (velocity.y * slideFactor))
        // 4
        //finalPoint.x = min(max(finalPoint.x, 0), self.view.bounds.size.width)
        //finalPoint.y = min(max(finalPoint.y, 0), self.view.bounds.size.height)

        // 5
        UIView.animate(withDuration: Double(slideFactor),
                       delay: 0,
                       // 6
            options: UIViewAnimationOptions.curveEaseOut,
            animations: {recognizer.view!.center = finalPoint },
            completion: nil)
    }

我的问题是: 当平移手势结束时,我可以查看动画。 直到动画结束,uiview才会收到我的触摸。

我的意思是只有在动画代码完成时才会出现另一个平移手势。

在动画结束之前,我的平移手势是否有任何方式识别触摸???

任何想法???

1 个答案:

答案 0 :(得分:2)

您应该为动画使用allowUserInteraction选项。

UIView.animate(withDuration: Double(slideFactor), delay: 0.0, options: [.curveEaseOut, .allowUserInteraction], animations: {
    recognizer.view?.center = finalPoint
}, completion: nil)