【发布时间】:2017-02-21 15:36:48
【问题描述】:
在我的 ViewController 视图中添加了平移手势。它必须检测到我点击了 collectionView 的哪个单元格,然后移动它。一切都很好,但有时 Pan Gesture 捕捉到“开始”和“改变”事件并且没有捕捉到任何结束事件(需要捕捉“结束”事件)。我试图捕捉所有事件,但是当我伸出手指时 - 什么也没有发生。在情节提要中添加了平移手势并与动作相关联。
这是我的操作代码:
//--------------\\
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
}
}
【问题讨论】:
-
“在情节提要上添加了平移手势并与动作相关”添加到什么?
-
@matt,IBAction
-
你没有回答我的问题。 to“在 Storyboard”中添加的平移手势识别器是什么?为某人提供足够的信息以重现您遇到的问题。
-
@matt,我的意思是我在代码中没有添加平移手势
-
没错。您将它添加到 视图中。 什么视图?
标签: swift uigesturerecognizer gesture uipangesturerecognizer