2016-12-24 7 views
0

私は、セルのいくつかの基本的なアニメーションを扱う基本クラスを持っています。私のサブクラスでは、レコグナイザターゲットをオーバーライドして他のものをやりたがっていますが、基本クラスを呼び出して.zeroにリセットされているため、変換は常に0です。UIPanGestureRecognizerのオーバーライド対象

基本アニメーションを行い、サブクラスを使ってメソッドをオーバーライドしたり、以下に示すように他のものを実行する方法はありますか?

私は、基底クラスを持っている:

class Base: UITableViewCell { 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan)) 
     addGestureRecognizer(gesture) 
    } 

    func handlePan(recognizer: UIPanGestureRecognizer) { 
     if recognizer.state == .changed { 
      let translation = recognizer.translation(in: self) 

      ... 

      recognizer.setTranslation(.zero, in: self) 
     } 
    } 
} 

そして、私のサブクラス:基底クラスで

class Sub: Base { 

    override func handlePan(recognizer: UIPanGestureRecognizer) { 
     super.handlePan(recognizer: recognizer) 

     if recognizer.state == .changed { 
      let translation = recognizer.translation(in: self) 

      print(translation.x) // is always 0 

      recognizer.setTranslation(.zero, in: self) 
     } 
    } 
} 

答えて

0

あなたは別の関数に共通の機能を分割だろうか?にサブクラスを防ぐ `:` recognizer.setTranslationを呼び出して、基本クラスを(自己.zero、中)を有する

class Base: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan)) 
     addGestureRecognizer(gesture) 
    } 

    func handlePan(recognizer: UIPanGestureRecognizer) { 
     if recognizer.state == .changed { 
      let translation = recognizer.translation(in: self) 
      performBaseAnimation(recognizer) 
      recognizer.setTranslation(.zero, in: self) 
     } 
    } 
    func performBaseAnimation(_ recognizer: UIPanGestureRecognizer) { 
     // perform common actions here 
    } 
} 
class Sub: Base { 
    override func handlePan(recognizer: UIPanGestureRecognizer) { 
     super.handleBaseAnimation(recognizer) 
     if recognizer.state == .changed { 
      // perform subclass actions here 
     } 
    } 
} 
+0

はまだ問題が同じになり、:あなたはsuper.handlePanを呼び出す必要がありませんその方法'recognizer.translation(in:)'を使用してください。 –

+0

私の例では、サブクラス内のものをオーバーライドしているので、スーパービュー** let **はサブクラスから呼び出されず、スーパービューだけが呼び出されます。サブクラスからの "スーパー"コールではどこにも見つかりません。 – dfd

+0

あなたは正しいです。私はちょうどそれをテストし、それは動作します。ありがとう! –

関連する問題