我正在嘗試將向右和向左滑動添加到集合視圖單元格以將容器以一定角度左右轉換
這是我的初始設定
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]
基于滑動位置移動會非常棒,我的意思是不會自動到達那個位置,用指尖移動,當它到達那里時,就停止移動。
誰能幫我實作它,我不知道我該怎么做
非常感謝
uj5u.com熱心網友回復:
添加completion
以UIView.animate
回傳初始狀態。一兩秒后取決于你duration
的UIView.animate
代碼將是這樣的
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
})
對于您的第二個問題,您可以使用touchesBegan
和touchesMoved
或添加PanGesture
來實作您想要的更改位置容器視圖框架。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512852.html
標籤:迅速cgaffine变换
上一篇:如何使用手動解碼撰寫列舉