我创建了一个滑块,该滑块从一个边缘移动到另一边缘,并在拖动后返回其原始位置。当到达幻灯片的末尾时,我调用一个函数,但现在面临的问题是,当移至末尾时,该函数被多次调用,而应该只调用一次。我如何使此滑块仅一次而不是多次进行调用,因为如果用户决定将其保持在最后,则该方法将被重复调用。 bleow是我到目前为止的代码
override func viewDidLoad() {
super.viewDidLoad()
let swipeGesture = UIPanGestureRecognizer(target: self, action: #selector(acknowledgeSwiped(sender:)))
swipeImage.addGestureRecognizer(swipeGesture)
swipeImage.isUserInteractionEnabled = true
swipeGesture.delegate = self as? UIGestureRecognizerDelegate
}
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
acknowledgeTrip()
}
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
})
}
}
}
任何帮助将不胜感激