【发布时间】:2018-09-27 04:46:32
【问题描述】:
我正在开发 UIPanGestureRecognizer,对我来说它正在工作。但我在这里遇到了一些问题,因为我是 iOS 新手,刚刚从 Android 转移到 iOS。 先看看我要做什么:
我想要什么:我有一个 UITableView,我想在单元格上执行滑动。我只想将它们从左向右拖动并移动/删除该单元格。与在android中完成的非常相似。
但我只想将项目移动到一个方向。那就是“从左到右”。但不是从右到左。现在在这里看看我到目前为止做了什么
我做了什么:
@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 值吗?或者只是一些如何让用户想要从右向左滑动并阻止用户这样做?请分享你的知识
【问题讨论】:
-
你应该尝试使用UISwipeGesture而不是UIPanGesture
-
我为什么要使用这个?
-
给我它的例子,或者教程
-
@QuocNguyen 是对的,您必须在 uitableviewcell 上执行 Uiswipegesture 并且您不需要任何 cgpoint 只需使用交换手势.direction == 的委托方法然后执行您的任务
-
感谢@HimanshuMoradiya 这很棒
标签: ios swift4 xcode9 uipangesturerecognizer