简体   繁体   English

在UITableViewCell中滑动UIView

[英]Sliding a UIView in a UITableViewCell

I'm creating a slidable UIView called "containerView" in each cell of a UITableView. 我正在UITableView的每个单元格中创建一个称为“ containerView”的可滑动UIView。 However, when i slide a containerView in a specific cell, all containerViews in all cells slide with it. 但是,当我在特定的单元格中滑动一个containerView时,所有单元格中的所有containerViews都会随之滑动。 Here is my code: 这是我的代码:

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
}

and this is my drag function: 这是我的拖动功能:

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)
    }
}

I want each containerView in each cell to be independent of the other. 我希望每个单元格中的每个containerView彼此独立。 Any help? 有什么帮助吗? Thanks! 谢谢!

Try replacing this code in cellForRowAt method 尝试在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
}

and in wasDragged(gestureRecognizer: UIPanGestureRecognizer) method 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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM