2017-07-18 49 views
0

我想在UItableviewCell中左右划动UIview,并且我已经使用UIPanGestureRecognizer来实现此目的,下面是附加的源代码。然而,当我试图滚动UItableview时,我的Uiview可以上下移动,无法滚动到UItableviewcell,即Superview.Can任何人都可以告诉我如何管理?提前致谢?在UItableviewcell中的pangesture,表滚动不在swift中工作

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return 10 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "CardSwipeTVCell") as! CardSwipeTVCell 


    let tapGesture : UIPanGestureRecognizer! 
    tapGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.tapEdit(_:))) 
    cell.cardView.addGestureRecognizer(tapGesture!) 
    cell.cardView.tag = indexPath.row 
    tapGesture!.delegate = self 


    cell.selectionStyle = .none 
    return cell 


} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 


} 

func tapEdit(_ recognizer: UIPanGestureRecognizer) 
{ 
    print(recognizer.view?.tag as Any) 

    let sender = recognizer.view?.tag as Any 

    let indexPath = IndexPath(row: sender as! Int, section: 0) 
    let cell = self.cardTableView.cellForRow(at: indexPath) as? CardSwipeTVCell 


    cell?.cardView.backgroundColor = UIColor.black 

    let card = recognizer.view! 
    let point = recognizer.translation(in: view) 
    card.center = CGPoint(x: (cell?.center.x)! + point.x, y: (cell?.center.y)! + point.y) 

    if recognizer.state == UIGestureRecognizerState.ended 
    { 
     UIView.animate(withDuration: 0.3, animations: { 

      card.center = (cell?.center)! 

     }) 

    } 

} 

另一个问题是,当我点击第二个单元我的视图消失了?你的帮助将非常感激。

回答

0
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool { 
    if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer { 
     //let translation = panGestureRecognizer.translation(in: superview!) 
     let translation = panGestureRecognizer.translationInView(superview) 
     if fabs(translation.x) > fabs(translation.y) { 
      return true 
     } 
     return false 
    } 
    return false 
} 

将上面的委托方法添加到您的类中。这将停止pangesture而tableview视图垂直滚动。

相关问题