2017-04-25 6 views
0

私は内部に2つの機能を持つUIPanGestureRecognizerセットアップを持っています。私はボタン内でこれらの機能を参照できるようにしたい。別のfunc(swift)内にあるfuncを参照する

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

ありがとうございました。私はletカード= sender.viewと共にスコープの外にそれらを移動しました!しかし、未解決の識別子 'sender'をそのletで使用するとエラーになります。 –

+0

関数にパラメータを追加します。 'func swipeRight(ビュー:NSView)'は 'sender'をパラメータとして渡し、' card'の代わりに 'view'を使います。 – Oskar

+0

NSViewを使用している場合は、「宣言されていないタイプのNSViewを使用」を取得します。私はそれが少し混乱を見つけるように迅速に迅速に新しいです! –

関連する問題