日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

swift 拖动按钮_Swift - 单元格滑动按钮库SwipeCellKit使用详解1(基本用法)

發布時間:2025/3/15 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 swift 拖动按钮_Swift - 单元格滑动按钮库SwipeCellKit使用详解1(基本用法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在之前的兩篇文章中我分別介紹了如何使用 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 UIKit

import SwipeCellKit

class 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 = self

self.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! SwipeTableViewCell

cell.delegate = self

cell.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 in

UIAlertController.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 in

UIAlertController.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()

}

}

//擴展UIAlertController

extension 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 in

UIAlertController.showAlert(message: "點擊了“未讀”按鈕")

}

//設置按鈕的文字顏色和字體大小

unreadAction.textColor = .green

unreadAction.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 in

UIAlertController.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 in

UIAlertController.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)

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的swift 拖动按钮_Swift - 单元格滑动按钮库SwipeCellKit使用详解1(基本用法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。