简体   繁体   English

从另一个 function 调用leadingSwipeActionsConfigurationForRowAt

[英]Call leadingSwipeActionsConfigurationForRowAt from another function

An unusual question, I have a UITableViewCell that have both leading and trailing swipe actions,一个不寻常的问题,我有一个 UITableViewCell 有前导和尾随滑动动作,

Is there a way to call one from another?有没有办法从另一个调用一个?

For example the user swiped the cell and leading action appeared, on clicking on it, the trailing action will appear.例如,用户滑动单元格并出现前导动作,单击它时,将出现尾随动作。

As in the attached photo here如附图所示滑动动作

When the user swipe an icon will appear from the leading edge of the cell, once clicked, the trailing action will appear with a confirmation button to delete the cell.当用户滑动时,单元格的前缘会出现一个图标,一旦点击,就会出现尾部动作,并带有一个删除单元格的确认按钮。

Is that possible to achieve?这有可能实现吗?

Here is my code:这是我的代码:

    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let leadingAction = UIContextualAction(style: .normal, title: "", handler: { (leadingAction, view, completionHandler) in
        // Action here
        completionHandler(true)
    })

    if let deleteImage =  UIImage(named: "Delete")?.cgImage {
         leadingAction.image = ImageWithoutRender(cgImage: deleteImage, scale: UIScreen.main.nativeScale, orientation: .up)
    }

    let preventFullSwipe = UISwipeActionsConfiguration(actions: [leadingAction])
    preventFullSwipe.performsFirstActionWithFullSwipe = false


    leadingAction.backgroundColor = Colors.clearAlpha
    let configuration = UISwipeActionsConfiguration(actions: [leadingAction])
    return configuration
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let trailingAction = UIContextualAction(style: .normal, title: "", handler: { (trailingAction, view, completionHandler) in
        // Action here
        completionHandler(true)
    })

    if let deleteImage =  UIImage(named: "ComfirmDelete")?.cgImage {
         trailingAction.image = ImageWithoutRender(cgImage: deleteImage, scale: UIScreen.main.nativeScale, orientation: .up)
    }

    trailingAction.backgroundColor = Colors.clearAlpha
    let configuration = UISwipeActionsConfiguration(actions: [trailingAction])
    return configuration
}

You can simulate gesture of swipe by adding following lines in trailing action completion block您可以通过在尾随动作完成块中添加以下行来模拟滑动手势

let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: youtableViewCell)
wasDragged(gesture)

further behavior.进一步的行为。

func wasDragged (gesture: UIPanGestureRecognizer) {        
    let translation = gesture.translationInView(youtableViewCell)
    let cell = gesture.view!

    // Move the object depending on the drag position
    cellcontentView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
                              y:  self.view.bounds.height / 2 + translation.y)
}

Note: It may be not a perfect solution but you can make it work you have a guide now:)注意:它可能不是一个完美的解决方案,但你可以让它工作你现在有一个指南:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM