Robin Hi,
The solution have turned out simple. Just add "layer" in self.layer.frame and everything is going to be ok.
All the code should looks like this:
The first part -
@objc func handlePan(gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began:
self.superview?.subviews.last?.layer.removeAllAnimations()
case .changed:
handleChanged(gesture)
case .ended:
handleEnded(gesture)
default:
()
}
}
and the second
fileprivate func handleEnded(_ gesture: UIPanGestureRecognizer) {
//set up functionality to dismiss the card from left to right and backwards
let translationDirection: CGFloat = gesture.translation(in: nil).x > 0 ? 1 : -1
let shouldDismissCard = abs(gesture.translation(in: nil).x) > threshold
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.1, options: .curveEaseOut, animations: {
if shouldDismissCard {
self.layer.frame = CGRect(x: 600 * translationDirection, y: 0, width: self.frame.width, height: self.frame.height)
//self.superview?.subviews.last?.layer.removeAllAnimations()
} else {
// bringing back to starting point
self.transform = .identity
}
}) { (_) in
self.transform = .identity
if shouldDismissCard {
self.removeFromSuperview()
}
}
}