Reputation: 958
I'm translating a view using a UIPanGestureRecognizer method:
@objc fileprivate func handlePan(_ gestureRecognizer : UIPanGestureRecognizer){
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
makeDisappearButtons()
deletebox.isHidden = false
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
gestureRecognizer.view!.transform = .init(scaleX: 0.6, y: 0.6)
self.deletebox.transform = .init(scaleX: 1.2, y: 1.2)
}, completion: nil)
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.deletebox.transform = .identity
gestureRecognizer.view!.transform = .identity
}, completion: nil)
}
}
if gestureRecognizer.state == .ended {
makeButtonsReAppear()
if gestureRecognizer.view!.frame.intersects(deletebox.frame) {
gestureRecognizer.view!.isHidden = true
gestureRecognizer.view?.removeFromSuperview()
} else {
gestureRecognizer.view!.setNeedsDisplay()
gestureRecognizer.setTranslation(.zero, in: self.view)
gestureRecognizer.view!.transform = .identity
}
print(gestureRecognizer.translation(in: self.view))
deletebox.isHidden = true
}
}
I want to know the translation of the view in gestureRecognizer.state == .ended but when I try to print it out it is x:0, y:0.
Upvotes: 1
Views: 696
Reputation: 30426
You're setting the translation back to .zero
every time the gesture handler calls changed
.
gestureRecognizer.setTranslation(.zero, in: self.view)
However, the state changes to ended
when the finger lifts up, and by that time the translation doesn't change anymore.
The documentation says:
When the user’s fingers lift from the screen, the gesture recognizer enters the UIGestureRecognizer.State.ended state.
So the translation is the same on the last changed
call and the ended
-- and because you're already setting the frame of gestureRecognizer.view
inside changed
, you don't need to do anything inside ended
.
Upvotes: 1
Reputation: 160
From where have you called this method, may be put it in viewDidAppear and try. Otherwise call setNeedsDisplay and LayoutIfNeeded methods.
Upvotes: 0