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在每個單元獨立於其他的。任何幫助?謝謝!
做你給每個視圖任何標籤?你可以通過標籤來完成,特定的標籤視圖將會滑動。 – Manoj