简体   繁体   English

如何在 swift 5 中实现滑动中的两个垂直按钮以删除

[英]How can implement two vertical button in swipe to delete in swift 5

在此处输入图像描述 I am trying to implement swipe to delete feature with two options, one is to delete and another one is to edit.我正在尝试使用两个选项实现滑动删除功能,一个是删除,另一个是编辑。 The things I want is these options should be vertical rather than horizontal.我想要的是这些选项应该是垂直的而不是水平的。 Thanks in advance for support.提前感谢您的支持。

You can easily achieve this swipe to reveal option feature using Custom TablViewCell您可以使用Custom TablViewCell轻松实现此滑动以显示选项功能

Design a view with two buttons and add a swipe gesture to the view to reveal the vertically aligned buttons设计一个带有两个按钮的视图,并向视图添加滑动手势以显示垂直对齐的按钮

Anyway, I think you would rather use default method editActionsForRowAt for similar cases.无论如何,我认为您宁愿在类似情况下使用默认方法editActionsForRowAt If not I hope this code will help you.如果没有,我希望这段代码对您有所帮助。

class TableViewController: UIViewController {

let tableView: UITableView = {
    let tableView = UITableView()
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.register(CustomCell.self,
                              forCellReuseIdentifier: CustomCell.identifier)
    
    return tableView
}()

var selectedCell: CustomCell?

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .white
    
    view.addSubview(tableView)
    setupConstraints()
    
    tableView.dataSource = self
    tableView.delegate = self
    tableView.reloadData()
}

func setupConstraints() {
    NSLayoutConstraint.activate([
        tableView.topAnchor.constraint(equalTo: view.topAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
    ])
}
}

extension TableViewController: UITableViewDelegate & UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath) as? CustomCell else {
        return UITableViewCell()
    }
    
    cell.delegate = self
    
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    44
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    selectedCell?.setInitialState()
}
}

extension TableViewController: CustomCellDelegate {
func didUpdateState(customCell: CustomCell?) {
    if customCell != selectedCell {
        selectedCell?.setInitialState()
    }
    selectedCell = customCell
}
}

protocol CustomCellDelegate: class {
    func didUpdateState(customCell: CustomCell?)
}

class CustomCell: UITableViewCell {

weak var delegate: CustomCellDelegate?

static let identifier = "Cell"

private let customViewWidth: CGFloat = 100

private let customView: CustomView = {
    let view = CustomView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .yellow
    return view
}()

private let fakeContentView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

enum CustomCellState {
    case hiddenCustomView
    case showedCustomView
}

private var trailingConstraint = NSLayoutConstraint()
private var cellState: CustomCellState = .hiddenCustomView

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = .yellow
    
    let panGesture = UIPanGestureRecognizer(target: self, action:(#selector(handleGesture(_:))))
    fakeContentView.addGestureRecognizer(panGesture)
    
    contentView.addSubview(fakeContentView)
    fakeContentView.addSubview(customView)
    setConstraints()
    updateCellState()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setInitialState() {
    self.trailingConstraint.constant = 0
    UIView.animate(withDuration: 0.5, animations: {
        self.contentView.layoutIfNeeded()
    })
}

@objc private func handleGesture(_ recognizer: UIPanGestureRecognizer) {
    let location = recognizer.location(in: fakeContentView)
    let dx = frame.width - location.x
    
    updateFrame(deltaX: dx)
    
    if recognizer.state == .ended {
        cellState = dx > customViewWidth / 2 ? .showedCustomView : .hiddenCustomView
        updateCellState()
        delegate?.didUpdateState(customCell: self)
    }
}

private func updateFrame(deltaX: CGFloat) {
    
    guard abs(deltaX) <= customViewWidth else {
        return
    }
    
    trailingConstraint.constant = -deltaX
    contentView.layoutIfNeeded()
}

private func updateCellState() {
    let dx: CGFloat = cellState == .hiddenCustomView ? 0 : customViewWidth
    self.trailingConstraint.constant = -dx
    
    UIView.animate(withDuration: 0.5, animations: {
        self.contentView.layoutIfNeeded()
    }, completion: nil)
}

private func setConstraints() {
    trailingConstraint = fakeContentView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
    trailingConstraint.isActive = true
     
    NSLayoutConstraint.activate([
        fakeContentView.topAnchor.constraint(equalTo: contentView.topAnchor),
        fakeContentView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        fakeContentView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        
        customView.topAnchor.constraint(equalTo: fakeContentView.topAnchor),
        customView.bottomAnchor.constraint(equalTo: fakeContentView.bottomAnchor),
        customView.leadingAnchor.constraint(equalTo: fakeContentView.trailingAnchor),
        customView.widthAnchor.constraint(equalToConstant: customViewWidth)
    ])
}
}

class CustomView: UIView {

private let button1: UIButton = {
    let button = UIButton(type: .custom)
    button.backgroundColor = .blue
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

private let button2: UIButton = {
    let button = UIButton(type: .custom)
    button.backgroundColor = .black
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = .gray
    addSubview(button1)
    addSubview(button2)
    setConstraints()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

private func setConstraints() {
     
    NSLayoutConstraint.activate([
        button1.topAnchor.constraint(equalTo: topAnchor),
        button1.leadingAnchor.constraint(equalTo: leadingAnchor),
        button1.trailingAnchor.constraint(equalTo: trailingAnchor),
        button1.bottomAnchor.constraint(equalTo: centerYAnchor),
        
        button2.bottomAnchor.constraint(equalTo: bottomAnchor),
        button2.leadingAnchor.constraint(equalTo: leadingAnchor),
        button2.trailingAnchor.constraint(equalTo: trailingAnchor),
        button2.topAnchor.constraint(equalTo: button1.bottomAnchor)
    ])
}
}

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

相关问题 如何在 ios 中实现两个垂直按钮在滑动中删除? - How can implement two vertical button in swipe to delete in ios? 滑动删除按钮的自定义高度 - custom height for swipe delete button swift 如何快速实施“删除”通知 - how can I implement a “delete” notification in swift 如何为 UICollectionView 实现 UITableView 的滑动删除 - How to implement UITableView`s swipe to delete for UICollectionView Swift - 在静态UITableView中滑动删除(不带删除按钮) - Swift - Swipe to delete (without the delete button) in a static UITableView 如何在tableview编辑模式下滑动删除? (迅速) - How to swipe to delete in tableview editing mode? (swift) Swift-如果检测到“滑动删除”,如何在自定义tableViewCell类中隐藏按钮? - Swift - How to hide button in custom tableViewCell class if left “Swipe to delete” detected? 如何让 iOS 在 tableView 上显示删除按钮,但不允许从右侧完全滑动删除 - How can I get iOS to show a delete button on a tableView, but not allow a full swipe from the right to delete 如何在UITableViewCell上使滑动以删除按钮更小? - How to make swipe to delete button smaller on UITableViewCell? 如何实现左右滑动以在 swift IOS 中更改数据/视图? - How can I implement Swipe left and right to change data/views in swift IOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM