2017-04-27 1 views
-1

Google Nowカードのようにtableviewセルを削除するにはスワイプを実装する必要があります。私はタッチイベントを通してその論理に挑戦しました。しかし、Google Nowのようにそれを取得しませんでした。それほど滑らかではありません。 はFYI、私は私のカスタムに次のコードを入れている。ここタッチ終了イベントをiOS Swift:Google NowカードのようにスワイプでTableViewCellを削除

func getGestureDirectionWithTouch(touch:UITouch) -> SwipeDirection{ 

     let gestureEndPoint = touch.location(in: self) 
     let dx = fabs((self.gestureStartPoint?.x)! - gestureEndPoint.x); 
     let dy = -1 * (gestureEndPoint.y - (self.gestureStartPoint?.y)!); 

     if(dx > 20) { 
      // left/right 
      return .Right 
     } 

     if(dy < 0) { 
      // down 
      return .Down 
     }else if(dy > 0) { 
      // up 
      return .Up; 
     } 

     return .none 
    } 



    // MARK: - Touch Events 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     print(#function) 
     super.touchesBegan(touches, with: event) 
     if let touch = touches.first { 

      self.gestureStartPoint = touch.location(in: self) 
     } 

    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     print(#function) 
     if let touch = touches.first { 

      //gets gesture direction 
      self.gestureDirection = self.getGestureDirectionWithTouch(touch: touch) 

      //send event 
      if (self.gestureDirection == .Left || self.gestureDirection == .Right) { 
       //exit if view is self or if view can't swipe 
       // if self.gestureView == self || !self.canSwipe(view: gestureView!) { 
       // return; 
       //} 

       //swipe card 
       let gestureEndPoint = touch.location(in: self) 
       self.frame = (self.frame).offsetBy(dx: (gestureEndPoint.x - (self.gestureStartPoint?.x)!), 
                       dy: (0)) 
       if ((self.alpha) > CGFloat(0.4)) { 
        self.alpha=(self.alpha)-0.01 
       } 
      } 
     } 
    } 
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     print(#function) 


      //we are swiping a card 
      let x = self.frame.origin.x 

      if fabs(Double(x)) > Double(self.frame.size.width/2){ 

       //card will be deleted when x is greater than half width view 

      }else{ 
       //card will be positioned at the orginila position 
       self.center = self.cellCenter! 
       self.alpha=1.0; 
      }  

    } 

をtableviewcellよりoftenlyトリガ。私は滑らかさがそこにない理由と思う。 Google Nowカードのようにtableviewcellをtableviewから削除するためのスワイプを実現する最善の方法を教えてください。

ありがとうございます。

+1

テーブルビューセルにスワイプで削除する機能があります。このリンクはあなたを助けるかもしれません:http://stackoverflow.com/questions/3309484/uitableviewcell-show-delete-button-on-swipe。タッチデリゲートで遊ぶ必要はありません。これがあなたを助けることを願っています! –

+0

@PallaviSrikhakollu、Google Nowアプリのように見えますか?セルをスワイプするときに削除ボタンを表示する必要はないからです。 – Sridhar

答えて

0

OK、私は

あなたの質問は、あなたがそれが「を削除するにはスワイプ」だろう一定の距離をパンする場合を除き、まったく同じ機能を使用することになり、ボタンを表示するためのカスタムスワイプで昨日同様の質問に答え、そう...ここでは、私はそのに多くのコードを知っている

var savedX = 0 as CGFloat 
var buttonWidth = 60 as CGFloat 
var open = false 

func panGestureHandler(gesture: UIPanGestureRecognizer) { 
if gesture.state == .Changed { 
    let translation = gesture.translationInView(tagView) 

    let difference = -translation.x 

    if difference > 0 && !allowScrollRight { 
     return 
    } 

    let newConstant = savedX + difference 

    tagViewCenterXConstraint.constant = newConstant 

    let alpha = abs(tagViewCenterXConstraint.constant)/buttonWidth 

    deleteButton.alpha = min(alpha, 1) 
    followButton.alpha = min(alpha, 1) 

    if let action = swipe { 
     action(self) 
    } 
} 

if gesture.state == .Ended { 
    let translation = gesture.translationInView(self) 

    let trans = fabs(translation.x) 

    open = !open && trans > buttonWidth 

    if open { 
     if(translation.x > 0){ 
      resetRight(true) 
     } else { 
      if allowScrollRight { 
       resetLeft(true) 
      } 
     } 
    } else { 
     resetView(true){ 

     } 
    } 
} 
} 
func resetLeft (animated : Bool) { 
     tagViewCenterXConstraint.constant = self.buttonWidth 
     savedX = self.buttonWidth 
if animated { 
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: {() -> Void in 
     self.tagView.layoutIfNeeded() 
     self.leftView.layoutIfNeeded() 
     self.rightView.layoutIfNeeded() 

     }, completion: { (finished) -> Void in 
    }) 
} 
} 
func resetRight (animated : Bool) { 
    tagViewCenterXConstraint.constant = -self.buttonWidth 
    savedX = -self.buttonWidth 

if animated { 
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: {() -> Void in 
     self.tagView.layoutIfNeeded() 
     self.leftView.layoutIfNeeded() 
     self.rightView.layoutIfNeeded() 

     }, completion: { (finished) -> Void in 
    }) 
} 
} 
func resetView (animated : Bool, completion:() -> Void) { 
    tagViewCenterXConstraint.constant = 0 

savedX = 0 
open = false 

if animated { 
    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [UIViewAnimationOptions.CurveEaseIn, UIViewAnimationOptions.BeginFromCurrentState], animations: {() -> Void in 
     self.tagView.layoutIfNeeded() 
     self.leftView.layoutIfNeeded() 
     self.rightView.layoutIfNeeded() 

     }, completion: { (finished) -> Void in 
      completion() 
    }) 
} else { 
    completion() 
    } 
} 

をボタンを表示するためにパンジェスチャーを設定しているが、それはあなたが何をする必要があるか、それは今、何をするかであることで、翻訳の速度と距離を確認していますジェスチャの状態が変更されたとき、これが一定のしきい値を超えたときにスワイプを呼び出して関数を削除すると、そのスクロールからパンのビューがアニメートされますnセルをVCに渡してdeleteCellAtIndexPathを呼び出します。これによってセルが削除され、ビューからモデルを削除し、モデルを更新するための関連API呼び出しも削除されます。

+0

ありがとう@ SeanLintern88、このコードをチェックさせてください – Sridhar

関連する問題