滑块最大值调用多个方法Swift

时间:2019-02-16 14:22:05

标签: swift

我创建了一个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
                })
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式进行操作:
1.添加成员

var sliderSessionActive: Bool = False
var sliderFunctionCalled: Bool = False
  1. 将事件.ended提取到不同的情况下。
  2. 管理sliderSessionActive:在.began中将其设置为“ True”,在False中将其设置为.ended
  3. 在调用函数时,将sliderFunctionCalled设置为True,并在False中将.ended设置为
  4. 仅在满足以下条件时调用函数:sliderSessionActive and not sliderFunctionCalled