如何获得类似于iOS的本机Mail应用程序的删除功能

时间:2016-05-12 20:11:05

标签: ios swift

我有一个删除按钮,在刷卡时变得可见,但我真的很喜欢邮件应用程序中发生的事情,如果你继续刷卡,将使用删除按钮,删除该项目。 我已经看过很多关于使用

实现项目的教程
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 

但我无法弄清楚使用什么来使按钮扩展到表格单元格的整个宽度。有什么想法吗?

A look at what I want to implement

编辑:我决定只在删除密钥显示后禁用刷卡,我不喜欢你如何刷表格单元格以查看底层背景。

1 个答案:

答案 0 :(得分:1)

使用UIScrollView分页可以实现这一点。

创建UIScrollView并将其作为子视图添加到UITableViewCell (还要添加大小约束以使scrollview与其父边界大小相同)

现在为scrollview设置此代码:

//set scroll view
scrollview.delegate = self;
scrollview.pagingEnabled = true;
scrollview.showsHorizontalScrollIndicator = false;
scrollview.showsVerticalScrollIndicator  = false;
scrollview.contentSize.width = scrollview.bounds.width * 2.0;

//create delete swipe view
let deleteButton = UIView(frame: CGRectMake(scrollview.bounds.width,0,scrollview.bounds.width * 2.0,scrollview.bounds.height))
deleteButton.backgroundColor = UIColor.redColor()

let deleteLabel = UILabel(frame: CGRectMake(0,0,scrollview.bounds.width * 0.3,scrollview.bounds.height))
deleteLabel.textColor = UIColor.whiteColor()
deleteLabel.text = "Delete"
deleteLabel.contentMode = .Center
deleteLabel.textAlignment = .Center
deleteButton.addSubview(deleteLabel)
scrollview.addSubview(deleteButton)

现在是委托功能:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    if scrollView.contentOffset.x == 0{
        return;
    }

    let confirmDelete = UIAlertController(title: "Are you sure you want to delete?", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    confirmDelete.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Destructive, handler: { (_) -> Void in
       //Delete confirmed

    }))

    confirmDelete.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
        scrollView.contentOffset.x = 0;
    }))

    rootViewController.presentViewController(confirmDelete, animated: true, completion: nil);
}

这会产生下一个效果:

enter image description here