2017-04-25 160 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'。我真的很新,所以觉得有点混乱! –

相关问题