在我的ViewController视图中添加了平移手势。它必须检测,tapView的哪个单元被轻敲,然后移动它。一切都很好,但有时Pan Gesture抓住了#34;开始"并且"改变了#34;事件并没有捕获任何结束事件(需要捕获"结束"事件)。我试图捕捉所有事件,但当我伸出手指时 - 没有发生。平移手势在故事板上添加并与动作相关联。
以下是我的行动代码:
//--------------\\
var translitionSensitivity : CGFloat = 1.0
let thresholdWhenSensitivityWillGrow : CGFloat = self.view.frame.width / 4
let sensitivityMultiplier : CGFloat = 15
let animDuration = 0.4
//--------------\\
DispatchQueue.main.async {
let indexPath = self.getCellAtPoint(sender.location(in: self.view))
if indexPath == nil { return }
var cell = self.collectionView.cellForItem(at: indexPath!) as! DetailCardCollectionViewCell
if self.activeCell != nil { cell = self.activeCell } else {self.activeCell = cell}
let translation = sender.translation(in: self.collectionView)
var howMuchOffseted : CGFloat = 0.0
if sender.state == .began
{
self.lastTranslationPos = 0
}
if sender.state == .changed
{
if cell.mainViewTrailingConstraint.constant >= thresholdWhenSensitivityWillGrow
{
translitionSensitivity += (cell.mainViewTrailingConstraint.constant - thresholdWhenSensitivityWillGrow) / sensitivityMultiplier
}
else
{
translitionSensitivity = 1
}
howMuchOffseted = (self.lastTranslationPos - translation.x) / translitionSensitivity
self.lastTranslationPos = translation.x
cell.mainViewTrailingConstraint.constant += howMuchOffseted
}
if sender.state == .ended || sender.state == .cancelled || sender.state == .failed || sender.state == .possible || sender.state == .recognized
{
print("ended")
if cell.mainViewTrailingConstraint.constant > thresholdWhenSensitivityWillGrow / 2
{
cell.mainViewTrailingConstraint.constant = thresholdWhenSensitivityWillGrow
UIView.animate(withDuration: animDuration, delay: 0.0, options: [.curveEaseOut], animations: {
cell.layoutIfNeeded()
}, completion: nil)
}
else
{
cell.mainViewTrailingConstraint.constant = 0
UIView.animate(withDuration: animDuration, delay: 0.0, options: [.curveEaseOut], animations: {
cell.layoutIfNeeded()
}, completion: nil)
}
self.activeCell = nil
}
}
答案 0 :(得分:0)
你为自己制造的东西太难了。使用代码,将平移手势识别器放在视图上,您希望能够拖动(在每个单元格中)。或者,更好的是,将此视图包装在水平滚动视图中,以便它可以自动拖动(因为滚动视图是可滚动的) - 这就是大多数自制刷卡到删除实现的工作方式。