I want to achieve the scrollview like animation using pan gesture. I have achieved almost all properties of scrollview with the following code under pan gesture action method:
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)
}
MY QUESTION IS: When pan gesture ends I have the animation to view . Until that animation ends my touch is not received by uiview.
I mean another pan gesture occurs only when that animation code finishes.
Is there any way my pan gesture recognizes touch before that animation is over ???
Any ideas ???
You should use the allowUserInteraction
option for your animation.
UIView.animate(withDuration: Double(slideFactor), delay: 0.0, options: [.curveEaseOut, .allowUserInteraction], animations: {
recognizer.view?.center = finalPoint
}, completion: nil)