gpt4 book ai didi

swift - 引用另一个 func 中的 func (swift)

转载 作者:行者123 更新时间:2023-11-28 10:14:15 24 4
gpt4 key购买 nike

我有一个 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()
}

关于swift - 引用另一个 func 中的 func (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com