2017-03-06 25 views
0

我在TableViewCell中添加PanGesture。
但之後,我無法在TableView中滾動。
我得到了PanGesture與TableView中的滾動相撞。
我嘗試了其他的解決方案,但它仍然沒有工作。Tableviewcell中的PanGesture干擾在TableView中滾動查看

我的代碼如下。

class ChatlistCell: UITableViewCell{ 

var leftSwipe = UIPanGestureRecognizer() 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 

    setupForCell()  

} 

func setupForCell(){ 

    //SwipeButton 
    //Hide Button 
    rightHide.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:150, height:71) 
    rightHide.backgroundColor = UIColor(hex:"778295") 
    addSubview(rightHide) 

    HideLabel.frame = CGRect(x: 19.0, y: 28.0, width: 70, height: 15) 
    HideLabel.font = UIFont.systemFont(ofSize:15) 
    HideLabel.text = "Hide" 
    HideLabel.textColor = UIColor.white 
    rightHide.addSubview(HideLabel) 

    //Delete Button 
    rightDelete.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:75, height:71) 
    rightDelete.backgroundColor = UIColor(hex:"ff3600") 
    addSubview(rightDelete) 

    DeleteLabel.frame = CGRect(x: 25.0, y: 28.0, width: 70, height: 15) 
    DeleteLabel.font = UIFont.systemFont(ofSize:15) 
    DeleteLabel.text = "Delete" 
    DeleteLabel.textColor = UIColor.white 
    rightDelete.addSubview(DeleteLabel) 


    //Mute Button 
    leftMute.frame = CGRect(x:-150, y:0, width:150, height:71) 
    leftMute.backgroundColor = UIColor(hex:"5181e2") 
    addSubview(leftMute) 

    muteImg.frame = CGRect(x:97.5, y:20.5, width:30, height:30) 
    muteImg.image = UIImage(named:"chat_list_icon_notice_on") 
    leftMute.addSubview(muteImg) 

    //Pin Button 
    leftPin.frame = CGRect(x:-75, y:0, width:75, height:71) 
    leftPin.backgroundColor = UIColor(hex:"82a5e6") 
    addSubview(leftPin) 

    pinImg.frame = CGRect(x:22.5, y:20.5, width:30, height:30) 
    pinImg.image = UIImage(named:"chat_list_icon_pin_off") 
    leftPin.addSubview(pinImg) 


    leftSwipe = UIPanGestureRecognizer(target: self, action:#selector(self.swipe)) 
    leftSwipe.delegate = self 

    self.mainView.addGestureRecognizer(leftSwipe) 
} 

func swipe(recognizer: UIPanGestureRecognizer){ 

    let translation = recognizer.translation(in:self) 

    recognizer.setTranslation(CGPoint.zero, in: self) 
    print(translation) 

    if recognizer.state == UIGestureRecognizerState.began{ 

    } 

    if recognizer.state == UIGestureRecognizerState.ended{ 
    // self.mainView.center = CGPoint(x:mainView.center.x + translation.x , y:mainView.center.y) 
     if self.mainView.center.x <= UIScreen.main.bounds.width/2 - 76{ 

      UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in 
       self.mainView.frame = CGRect(x:-152.0, y:0, width:UIScreen.main.bounds.width, height:71) 
       self.rightHide.frame = CGRect(x:UIScreen.main.bounds.width - 152, y:0, width:152, height:71) 
       self.rightDelete.frame = CGRect(x:UIScreen.main.bounds.width - 76, y:0, width:76, height:71) 

      }, completion: nil) 

     }else if self.mainView.center.x >= UIScreen.main.bounds.width/2 + 76{ 

      UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in 
       self.mainView.frame = CGRect(x:152.0, y:0, width:UIScreen.main.bounds.width, height:71) 
       self.leftMute.frame = CGRect(x:0, y:0, width:152, height:71) 
       self.leftPin.frame = CGRect(x:0, y:0, width:76, height:71) 

      }, completion: nil) 


     }else{ 

      UIView.animate(withDuration: 0.2, delay: 0.0, options: [], animations:{ _ in 
       self.mainView.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:71) 
       self.rightHide.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:152, height:71) 
       self.rightDelete.frame = CGRect(x:UIScreen.main.bounds.width, y:0, width:76, height:71) 
       self.leftMute.frame = CGRect(x:-152, y:0, width:152, height:71) 
       self.leftPin.frame = CGRect(x:-76, y:0, width:76, height:71) 

      }, completion: nil) 
     } 

    } 

    if recognizer.state == UIGestureRecognizerState.changed{ 

     self.mainView.center = CGPoint(x:mainView.center.x + translation.x , y:mainView.center.y) 

     //left swipe 
     if self.mainView.center.x < UIScreen.main.bounds.width/2{ 

      if self.mainView.center.x <= UIScreen.main.bounds.width/2 - 152 { 
       self.rightHide.center = CGPoint(x:UIScreen.main.bounds.width - 76, y:mainView.center.y) 
       self.rightDelete.center = CGPoint(x: UIScreen.main.bounds.width - 38 , y: mainView.center.y) 
      }else{ 
       self.rightHide.center = CGPoint(x:rightHide.center.x + translation.x, y:mainView.center.y) 
       self.rightDelete.center = CGPoint(x: rightDelete.center.x + translation.x/2, y: mainView.center.y) 
      } 
      //right swipe 
     }else{ 

      if self.mainView.center.x >= UIScreen.main.bounds.width/2 + 152 { 
       self.leftMute.center = CGPoint(x: 76, y:mainView.center.y) 
       self.leftPin.center = CGPoint(x:38 , y: mainView.center.y) 
      }else{ 
       self.leftMute.center = CGPoint(x:leftMute.center.x + translation.x, y:mainView.center.y) 
       self.leftPin.center = CGPoint(x: leftPin.center.x + translation.x/2, y: mainView.center.y) 
      } 

     } 

    } else{ 

    } 

} 

下面是包含TableView的mainView類。

class ChatlistTabTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate{ 

@IBOutlet weak var tableView: UITableView! 


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

    let row = allChatroomList[indexPath.row] 

    let chatCell = tableView.dequeueReusableCell(withIdentifier: "chatCell") as! ChatListCell 

    chatCell.leftSwipe.delegate = self 
    return chatCell 
} 


func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool { 
    if gestureRecognizer is UITapGestureRecognizer { 
     let location = touch.location(in: tableView) 
     return (tableView.indexPathForRow(at: location) == nil) 
    } 
    return true 
} 
} 

回答