引用另一个功能(Swift)内的弹药



我有一个uipangeSturerecognizer设置,其中包含几个功能。我希望能够在按钮中引用这些功能。

uipangeSturerEcognizer

  @IBAction func panCard(_ sender: UIPanGestureRecognizer) {
    let card = sender.view!
    let point = sender.translation(in: view)
    card.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
    func swipeLeft() {
        //move off to the left
        UIView.animate(withDuration: 0.3, animations: {
            card.center = CGPoint(x: card.center.x - 200, y: card.center.y + 75)
            card.alpha = 0
        })
    }
    func swipeRight() {
        //move off to the right
        UIView.animate(withDuration: 0.3, animations: {
            card.center = CGPoint(x: card.center.x + 200, y: card.center.y + 75)
            card.alpha = 0
        })
    }
    if sender.state == UIGestureRecognizerState.ended {
        if card.center.x < 75 {
            swipeLeft()
            return
        } else if card.center.x > (view.frame.width - 75) {
            swipeRight()
            return
        }
        resetCard()
    }
}

和按钮

@IBAction func LikeButton(_ sender: UIButton) {
}

如何在按钮内引用任何功能Swipeleft和Swiperight?

这些功能无法从其 panCard函数内部的范围中访问。您唯一的选择是将它们移到范围外:

@IBAction func panCard(_ sender: UIPanGestureRecognizer) {
    let card = sender.view!
    let point = sender.translation(in: view)
    card.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
    if sender.state == UIGestureRecognizerState.ended {
        if card.center.x < 75 {
            swipeLeft()
            return
        } else if card.center.x > (view.frame.width - 75) {
            swipeRight()
            return
        }
    resetCard()
    }
}
func swipeRight() {
    //move off to the right
    UIView.animate(withDuration: 0.3, animations: {
        card.center = CGPoint(x: card.center.x + 200, y: card.center.y + 75)
        card.alpha = 0
    })
}
func swipeLeft() {
    //move off to the left
    UIView.animate(withDuration: 0.3, animations: {
        card.center = CGPoint(x: card.center.x - 200, y: card.center.y + 75)
        card.alpha = 0
    })
}
@IBAction func LikeButton(_ sender: UIButton) {
// swipeLeft()
// swipeRight()
}

最新更新