2017-01-02 57 views
0

我在UITableView的每個單元格中創建了一個名爲「containerView」的滑動UIView。但是,當我在特定的單元格中滑動containerView時,所有單元格中的所有containerView都會隨之滑動。這裏是我的代碼:在UITableViewCell中滑動UIView

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

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "TestCell") as! TestTableViewCell 
    cell.containerView.frame = CGRect(x: self.view.frame.size.width - 100, y:200, width: 225, height: 70) 
    cell.containerView.center.x = self.view.frame.size.width - 100 
    cell.containerView.backgroundColor = UIColor.black 
    cell.containerView.layer.cornerRadius = 20 
    let gesture = UIPanGestureRecognizer(target: self, action: #selector(wasDragged(gestureRecognizer:))) 
    cell.containerView.addGestureRecognizer(gesture) 
    cell.containerView.isUserInteractionEnabled = true 
    gesture.delegate = self 
    return cell 
} 

,這是我拖累功能:

func wasDragged(gestureRecognizer: UIPanGestureRecognizer) { 

    print("wassss dragggggeeed") 
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed { 
     let translation = gestureRecognizer.translation(in: self.view) 
     print(gestureRecognizer.view!.center.x) 
     let newx = gestureRecognizer.view!.center.x + translation.x 

     if(newx <= self.view.frame.width + 50) && (newx >= self.view.frame.width - 100) { 
      gestureRecognizer.view!.center = CGPoint(x: newx, y: gestureRecognizer.view!.center.y) 
     }else if (newx > self.view.frame.width + 50){ 
      gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width + 50, y: gestureRecognizer.view!.center.y) 
     }else if (newx < self.view.frame.width - 100){ 
      gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y) 
     } 

     gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view) 
    } 
} 

我希望每個containerView在每個單元獨立於其他的。任何幫助?謝謝!

+0

做你給每個視圖任何標籤?你可以通過標籤來完成,特定的標籤視圖將會滑動。 – Manoj

回答

0

嘗試在cellForRowAt方法替換該代碼

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! TestTableViewCell 

    cell.containerView.backgroundColor = UIColor.black 
    let gesture = UIPanGestureRecognizer(target: self, action: #selector(wasDragged(gestureRecognizer:))) 
    cell.containerView.addGestureRecognizer(gesture) 
    cell.containerView.isUserInteractionEnabled = true 

    return cell 
} 

wasDragged(gestureRecognizer:UIPanGestureRecognizer)方法

func wasDragged(gestureRecognizer: UIPanGestureRecognizer) { 

    let gestureView = gestureRecognizer.view! 
    let translation = gestureRecognizer.translation(in: self.view) 

    // manage translation as per your requirement 
    let newx = gestureRecognizer.view!.center.x + translation.x/10 

    if gestureRecognizer.state == .changed { 
     // while dragging 
     if newx < view.frame.size.width && newx >= view.frame.size.width/2 { 
      gestureView.center = CGPoint(x: newx, y: gestureRecognizer.view!.center.y) 
     } 
    } else if gestureRecognizer.state == .ended { 
     // when dragging ends 
     if newx >= view.frame.size.width { 
      gestureView.center = CGPoint(x: view.frame.size.width, y: gestureRecognizer.view!.center.y) 
     } else { 
      gestureView.center = CGPoint(x: view.frame.size.width/2, y: gestureRecognizer.view!.center.y) 
     } 
    } 
}