ai做漫画头像网站,商城网站制作公司地址,济南市住房与城乡建设厅网站,企业网站自助建设在之前的两篇文章中我分别介绍了如何使用 iOS8和 iOS11提供的相关代理方法#xff0c;来实现 tableView单元格滑动事件按钮#xff1a;但它们局限性还是比较大的#xff0c;前者只能实现尾部按钮#xff0c;且按钮只能使用文字无法使用图片。而后者对系统版本又要求比较高。…在之前的两篇文章中我分别介绍了如何使用 iOS8和 iOS11提供的相关代理方法来实现 tableView单元格滑动事件按钮但它们局限性还是比较大的前者只能实现尾部按钮且按钮只能使用文字无法使用图片。而后者对系统版本又要求比较高。下面介绍一个好用的第三方滑动单元格组件SwipeCellKit。不仅使用方便而且功能强大可以自由设置各种样式和动画效果。只要系统版本在iOS9.0以上就可以使用。一、基本介绍使用 SwipeCellKit可以很方便地实现类似系统里邮件 App那样的滑动效果。1功能特点支持左滑和右滑操作。动作按钮支持纯文本、文本图片以及纯图片样式。支持触觉反馈可自定义转场效果比如 Border、Drag以及 Reveal可自定义按钮滑动时的行为支持滑动超过一定范围时的自动展开动画可自定义自动展开动画2安装配置(2)将下载下来的源码包中 SwipeCellKit.xcodeproj拖拽至你的工程中(3)工程 - General- Embedded Binaries 项把 SwipeCellKit.framework添加进来。(4)最后在需要使用 SwipeCellKit的地方 import进来就可以了import SwipeCellKit二、使用样例1纯文字的滑动按钮(1)效果图我们在 tableView上向左滑动某个 cell时其右侧会出现“旗标”“删除”这两个按钮选项。当点击“旗标”按钮时页面上会弹出相关的操作信息。 而最右侧的“删除”按钮除了点击会触发外直接往左一滑到底也会触发触发后会将当前行数据给删除。 而右滑单元格时左侧会出现“未读”按钮点击后同样在页面上弹出相关的操作信息。 (2)样例代码import UIKitimport SwipeCellKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,SwipeTableViewCellDelegate{var tableView:UITableView?var items [这个是条目1,这个是条目2,这个是条目3,这个是条目4,这个是条目5,这个是条目6,这个是条目7,这个是条目8,]override func viewDidLoad() {super.viewDidLoad()//创建表格视图self.tableView UITableView(frame:self.view.frame, style:.plain)self.tableView!.delegate selfself.tableView!.dataSource self//创建一个重用的单元格self.tableView!.register(SwipeTableViewCell.self,forCellReuseIdentifier: SwiftCell)self.view.addSubview(self.tableView!)}//在本例中有1个分区func numberOfSections(in tableView: UITableView) - Int {return 1}//返回表格行数(也就是返回控件数)func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - Int {return items.count}//创建各单元显示内容(创建参数indexPath指定的单元)func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)- UITableViewCell {//为了提供表格显示性能已创建完成的单元需重复使用let identify:String SwiftCell//同一形式的单元格重复使用在声明时已注册let cell tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath) as! SwipeTableViewCellcell.delegate selfcell.textLabel?.text items[indexPath.row]return cell}//自定义滑动按钮func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath,for orientation: SwipeActionsOrientation) - [SwipeAction]? {//分别返回左侧、右侧的按钮if orientation .left {//创建“未读”事件按钮let unreadAction SwipeAction(style: .default, title: 未读) {action, indexPath inUIAlertController.showAlert(message: 点击了“未读”按钮)}unreadAction.backgroundColor UIColor(red: 52/255, green: 120/255,blue: 246/255, alpha: 1)//返回左侧事件按钮return [unreadAction]} else{//创建“旗标”事件按钮let favoriteAction SwipeAction(style: .default, title: 旗标) {action, indexPath inUIAlertController.showAlert(message: 点击了“旗标”按钮)}favoriteAction.backgroundColor .orange//创建“删除”事件按钮let deleteAction SwipeAction(style: .destructive, title: 删除) {action, indexPath in//将对应条目的数据删除self.items.remove(at: indexPath.row)tableView.reloadData()}//返回右侧事件按钮return [deleteAction, favoriteAction]}}//自定义滑动行为(可选)func tableView(_ tableView: UITableView,editActionsOptionsForRowAt indexPath: IndexPath,for orientation: SwipeActionsOrientation) - SwipeTableOptions {var options SwipeTableOptions()options.transitionStyle .border //变化样式(使用默认的不变)options.expansionStyle .selection //展开样式(默认为.none)return options}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}}//扩展UIAlertControllerextension UIAlertController {//在指定视图控制器上弹出普通消息提示框static func showAlert(message: String, in viewController: UIViewController) {let alert UIAlertController(title: nil, message: message, preferredStyle: .alert)alert.addAction(UIAlertAction(title: 确定, style: .cancel))viewController.present(alert, animated: true)}//在根视图控制器上弹出普通消息提示框static func showAlert(message: String) {if let vc UIApplication.shared.keyWindow?.rootViewController {showAlert(message: message, in: vc)}}}2修改文字的颜色和字体通过 SwipeAction的 textColor和 font属性我们可以分别修改按钮上文字的颜色和字体大小。//创建“未读”事件按钮let unreadAction SwipeAction(style: .default, title: 未读) {action, indexPath inUIAlertController.showAlert(message: 点击了“未读”按钮)}//设置按钮的文字颜色和字体大小unreadAction.textColor .greenunreadAction.font .systemFont(ofSize: 20)unreadAction.backgroundColor UIColor(red: 52/255, green: 120/255, blue: 246/255, alpha: 1)3带图标的滑动按钮(1)如果想要实现想邮件 App那样“图标 文字”的按钮我们只需要给对应的 SwipeAction设置个 image就可以了。//创建“未读”事件按钮let unreadAction SwipeAction(style: .default, title: 未读) { action, indexPath inUIAlertController.showAlert(message: 点击了“未读”按钮)}//设置按钮图标unreadAction.image UIImage(named: unread)unreadAction.backgroundColor UIColor(red: 52/255, green: 120/255, blue: 246/255, alpha: 1)(2)如果按钮只想要图标不需要文字标题的话把 title设置为 nil即可。//创建“未读”事件按钮let unreadAction SwipeAction(style: .default, title: nil) { action, indexPath inUIAlertController.showAlert(message: 点击了“未读”按钮)}//设置按钮图标unreadAction.image UIImage(named: unread)unreadAction.backgroundColor UIColor(red: 52/255, green: 120/255, blue: 246/255, alpha: 1)4自动展开事件按钮(1)上面的样例中不管是左侧还是右侧的事件按钮都是通过滑动手势来展开的。有时我们可能想通过程序来自动触发这个行为那么只要调用 SwipeTableViewCell 的 showSwipe 方法即可。(2)下面是一个简单的样例当我们点击某个 cell 时这个 cell 右侧的功能按钮便会自动出现。//点击某个单元格func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {let cell tableView.cellForRow(at: indexPath) as! SwipeTableViewCell//自动展开该单元格右侧的事件按钮cell.showSwipe(orientation: .right)}