滑动以解除像Gmail一样的撤消

时间:2016-12-14 12:45:24

标签: swift uitableview swipe-gesture

我想添加像this一样的滑动,实际上这个库是Android的。我想快速。

我使用了默认的tableview方法。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 

但问题是它只提供了一种左右滑动方式。

以及 GMAIL 应用程序中都有两次结束扫描,即使我们向该行滑动两次也会自动删除。

请指导我如何做到这一点。我无法找到任何第三方。

我创建的一件事是左右添加两个侧面手势然后在他们的方法中添加Button但没有任何事情发生。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {



        let moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{
            action, indexpath in

            print("MORE ACTION");

            self.Selected = "YES"

           self.selectedValue = indexpath.row

            self.tableView.rowHeight = 50

            self.tableView.beginUpdates()

            let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0)

            self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

            self.tableView.endUpdates()

        });

        moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);

        let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
            print("DELETE ACTION");

            self.tableView.endUpdates()

        });


    let Action = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "", handler:{action, indexpath in
        print("ACTION");
    });

    if self.Selected == "YES" {

        if self.selectedValue == indexPath.row {

            return [Action];
        }
        //return [Action];
        return [deleteRowAction, moreRowAction];
    }else{
    return [deleteRowAction, moreRowAction];
    }
}

和允许编辑的方法

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

           return false
    }

for gesture

self.tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell")

    let swipeRight = UISwipeGestureRecognizer(target: self, action:  #selector(ViewController.left(_:)))


    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.tableView.addGestureRecognizer(swipeRight)

    let swipeleft = UISwipeGestureRecognizer(target: self,  action: #selector(ViewController.right(_:)))

    swipeleft.direction = UISwipeGestureRecognizerDirection.Left

    self.tableView.addGestureRecognizer(swipeleft)



 func left(sender: UISwipeGestureRecognizer){
            print("left");
//what to write inside to make custom button here
        }
        func right(sender: UISwipeGestureRecognizer){
            print("right");
        }

2 个答案:

答案 0 :(得分:0)

使用UISwipeGesture和UITableviewCell实现此解决方案是一种简单的方法

导入UIKit

class TableViewCustomCell:UITableViewCell {

@IBOutlet weak var mainView: UIView!


override func awakeFromNib() {
    let leftSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:)))
    leftSwipe.direction = .Left;
    self.contentView.addGestureRecognizer(leftSwipe)

}

func swipe(touch: UIGestureRecognizer)
{
    let swipeGesture:UISwipeGestureRecognizer = sender as! UISwipeGestureRecognizer
    //Write your code to Unread or 
}
}

答案 1 :(得分:0)

一个好的方法是创建一个手势识别器,触发某个功能,您可以在其中部署自己的代码:

override func awakeFromNib() {        
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.leftSwiped))
    swipeLeft.delegate = self
    swipeLeft.numberOfTouchesRequired = 1
    swipeLeft.direction = .left
    self.tableView!.addGestureRecognizer(swipeLeft)
 }

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func leftSwiped() {

}