我创建了一个UI滑块,它可以工作,但是现在的问题是,当它滑动到返回之前滑到最后时,我将其称为函数,而现在多次调用了此函数。我希望此函数被调用一次。下面是我的代码
var startingFrame: CGRect?
@objc func acknowledgeSwiped(sender: UIPanGestureRecognizer) {
if let sliderView = sender.view {
let translation = sender.translation(in: swipeView)
switch sender.state {
case .began:
startingFrame = swipeImage.frame
fallthrough
case .changed:
if let startFrame = startingFrame {
var movex = translation.x
if movex < -startFrame.origin.x { movex = -startFrame.origin.x }
let xMax = swipeView.frame.width - startFrame.origin.x - startFrame.width
if movex > xMax {
movex = xMax
acknowledge()
}
var movey = translation.y
if movey < -startFrame.origin.y { movey = -startFrame.origin.y }
let yMax = swipeView.frame.height - startFrame.origin.y - startFrame.height
if movey > yMax {
movey = yMax
}
sliderView.transform = CGAffineTransform(translationX: movex, y: movey)
}
default: // .ended and others:
UIView.animate(withDuration: 0.1, animations: {
sliderView.transform = CGAffineTransform.identity
})
}
}
}
答案 0 :(得分:0)
您可以通过以下方式进行操作:
1.添加成员
var sliderSessionActive: Bool = False
var sliderFunctionCalled: Bool = False
.ended
提取到不同的情况下。sliderSessionActive
:在.began
中将其设置为“ True”,在False
中将其设置为.ended
。sliderFunctionCalled
设置为True
,并在False
中将.ended
设置为sliderSessionActive and not sliderFunctionCalled
。