UIPanGesture 识别器仅向一个方向滑动
UIPanGesture recognizer swiping only in one direction
我正在研究 UIPanGestureRecognizer,对我来说它正在工作。但我在这里遇到了一些问题,因为我是 iOS 的新手,刚从 Android 转移到 iOS。
先看看我要做什么:
我想要什么:我有一个 UITableView,我想在单元格上执行滑动。我只想将它们从左侧拖到右侧并 move/Delete 那个单元格。与在 android 中完成的非常相似。
但我只想在一个方向上移动项目。那就是 "LEFT TO RIGHT"。但不是从右到左。现在在这里看看我到目前为止所做的事情
我做了什么:
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
// 1
if recognizer.state == .began {
// when the gesture begins, record the current center location
originalCenter = center
print("Center",originalCenter)
}
// 2
if recognizer.state == .changed {
let translation = recognizer.translation(in: self)
center = CGPoint(x: originalCenter.x+translation.x, y: originalCenter.y)
// has the user dragged the item far enough to initiate a delete/complete?
deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
completeOnDragRelease = frame.origin.x > frame.size.width / 2.0
// print ("FrameX = ",frame.origin.x , " , ","Width = ",frame.size.width / 2.0 , "Total = ",frame.origin.x < -frame.size.width / 2.0 )
//print ("DelOnDrag = ",deleteOnDragRelease , " , ","CompOnDrag = ",completeOnDragRelease)
}
// 3
if recognizer.state == .ended {
// the frame this cell had before user dragged it
let originalFrame = CGRect(x: 0, y: frame.origin.y,
width: bounds.size.width, height: bounds.size.height)
if deleteOnDragRelease {
if delegate != nil && clickedItem != nil {
// notify the delegate that this item should be deleted
delegate!.toDoItemDeleted(clickedItem: clickedItem!)
}
} else if completeOnDragRelease {
UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
} else {
UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
}
}
}
我知道我可以检查“.changed”,并计算 X 值是否趋向于 0 或小于 0。但要点是一段时间内它会从右向左移动项目。
问题:有什么办法可以得到接触点的x值吗?或者我如何让用户想要从右向左滑动并阻止用户这样做?请分享您的知识
您可以使用以下扩展来做到这一点
extension UIPanGestureRecognizer {
enum GestureDirection {
case Up
case Down
case Left
case Right
}
func verticalDirection(target: UIView) -> GestureDirection {
return self.velocity(in: target).y > 0 ? .Down : .Up
}
func horizontalDirection(target: UIView) -> GestureDirection {
return self.velocity(in: target).x > 0 ? .Right : .Left
}
}
你可以得到如下的指示
gestureRecognizer.horizontalDirection(target: self)
你的相同代码只是你的 UIGestureRecognizer 方法中的一处更改替换为这段代码,你的问题就解决了。仅在您的 tableview 单元格上从左到右交换工作。任何对此进行重新评级的查询都只需在下面发表评论。
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
let translation = panGestureRecognizer.translation(in: superview!)
if translation.x >= 0 {
return true
}
return false
}
return false
}
祝你好运。
继续编码。
我正在研究 UIPanGestureRecognizer,对我来说它正在工作。但我在这里遇到了一些问题,因为我是 iOS 的新手,刚从 Android 转移到 iOS。 先看看我要做什么:
我想要什么:我有一个 UITableView,我想在单元格上执行滑动。我只想将它们从左侧拖到右侧并 move/Delete 那个单元格。与在 android 中完成的非常相似。
但我只想在一个方向上移动项目。那就是 "LEFT TO RIGHT"。但不是从右到左。现在在这里看看我到目前为止所做的事情
我做了什么:
@objc func handlePan(recognizer: UIPanGestureRecognizer) {
// 1
if recognizer.state == .began {
// when the gesture begins, record the current center location
originalCenter = center
print("Center",originalCenter)
}
// 2
if recognizer.state == .changed {
let translation = recognizer.translation(in: self)
center = CGPoint(x: originalCenter.x+translation.x, y: originalCenter.y)
// has the user dragged the item far enough to initiate a delete/complete?
deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
completeOnDragRelease = frame.origin.x > frame.size.width / 2.0
// print ("FrameX = ",frame.origin.x , " , ","Width = ",frame.size.width / 2.0 , "Total = ",frame.origin.x < -frame.size.width / 2.0 )
//print ("DelOnDrag = ",deleteOnDragRelease , " , ","CompOnDrag = ",completeOnDragRelease)
}
// 3
if recognizer.state == .ended {
// the frame this cell had before user dragged it
let originalFrame = CGRect(x: 0, y: frame.origin.y,
width: bounds.size.width, height: bounds.size.height)
if deleteOnDragRelease {
if delegate != nil && clickedItem != nil {
// notify the delegate that this item should be deleted
delegate!.toDoItemDeleted(clickedItem: clickedItem!)
}
} else if completeOnDragRelease {
UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
} else {
UIView.animate(withDuration: 8.2, animations: {self.frame = originalFrame})
}
}
}
我知道我可以检查“.changed”,并计算 X 值是否趋向于 0 或小于 0。但要点是一段时间内它会从右向左移动项目。
问题:有什么办法可以得到接触点的x值吗?或者我如何让用户想要从右向左滑动并阻止用户这样做?请分享您的知识
您可以使用以下扩展来做到这一点
extension UIPanGestureRecognizer {
enum GestureDirection {
case Up
case Down
case Left
case Right
}
func verticalDirection(target: UIView) -> GestureDirection {
return self.velocity(in: target).y > 0 ? .Down : .Up
}
func horizontalDirection(target: UIView) -> GestureDirection {
return self.velocity(in: target).x > 0 ? .Right : .Left
}
}
你可以得到如下的指示
gestureRecognizer.horizontalDirection(target: self)
你的相同代码只是你的 UIGestureRecognizer 方法中的一处更改替换为这段代码,你的问题就解决了。仅在您的 tableview 单元格上从左到右交换工作。任何对此进行重新评级的查询都只需在下面发表评论。
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
let translation = panGestureRecognizer.translation(in: superview!)
if translation.x >= 0 {
return true
}
return false
}
return false
}
祝你好运。
继续编码。