2017-04-25 33 views
0

我有一個UIPanGestureRecognizer安裝程序,裏面有幾個函數。我希望能夠在一個按鈕中引用這些功能。引用另一個func中的func(swift)

的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按鈕裏面?

回答

4

這些功能不能在您的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() 
} 
+0

好的謝謝。我已經將它們移出了範圍,並且讓card = sender.view!但後來得到錯誤的使用未解決的標識符'發件人'就讓那個。 –

+0

給函數添加一個參數: 'func swipeRight(view:NSView)',傳入'sender'作爲參數,並使用'view'而不是'card'。 – Oskar

+0

但如果使用NSView,我會'使用未聲明的類型NSView'。我真的很新,所以覺得有點混亂! –

相關問題