左右滑动后转换集合视图单元格容器



我正试图将向右和向左滑动添加到集合视图单元格中,以将容器转换为具有特定角度的左右

这是我的初始设置

private func setupGestures() {
let swipeToRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeRight))
swipeToRight.direction = .right
container.addGestureRecognizer(swipeToRight)

let swipeToLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeLeft))
swipeToLeft.direction = .left
container.addGestureRecognizer(swipeToLeft)

}

@objc func respondToSwipeRight(gesture: UIGestureRecognizer) {

let angle = CGFloat(Double.pi/2)

UIView.animate(withDuration: 0.5) {
self.container.transform = CGAffineTransform(rotationAngle: angle)
}
}


@objc func respondToSwipeLeft(gesture: UIGestureRecognizer) {

let angle = -CGFloat(Double.pi/2)

UIView.animate(withDuration: 0.5) {
self.container.transform = CGAffineTransform(rotationAngle: angle)
}
}

但它完全旋转了容器,这是我不想要的,我想让它像它一样,在一两秒后回到它的初始位置

[![它应该如何转换][1]][1]

基于Swiping位置的移动会非常棒,我的意思是不会自动移动到那个位置,用指尖移动,当它到达那里时,就停止移动。

有人能帮我实现它吗?我不知道我该怎么做

非常感谢

UIView.animate中添加completion以返回初始状态。一两秒钟后取决于您在UIView.animate中的duration

代码将像这个

UIView.animate(withDuration: 0.5, animations: {
self.container.transform = CGAffineTransform(rotationAngle: 0.5)
}, completion: {
_ in
// turn back to the previous before transform
self.container.transform = .identity
})

对于您的第二个问题,您可以使用touchesBegantouchesMoved来实现,或者添加PanGesture来处理您想要的更改位置的容器视图框架。

最新更新