我正在UITableView的每个单元格中创建一个名为“containerView”的可滑动UIView。但是,当我在特定单元格中滑动containerView时,所有单元格中的所有containerView都会随之滑动。这是我的代码:
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 :(得分: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)
}
}
}