UITableView以编程方式调用滑动操作

问题描述 投票:13回答:6

我有一个UITableView,用户可以向左滑动以显示操作(如在iOS 8邮件中)。这一切都按预期工作。我想在用户点击单元格的某个部分时触发此操作。如何以编程方式调用此幻灯片操作?

当前行为:用户必须向左滑动单元格以显示操作按钮。

期望的行为:用户点击单元格上的(操作按钮)。单元格滑过以显示操作按钮。

ios uitableview ios8
6个回答
16
投票

好吧,我找不到以编程方式执行此操作的方法,但我想出了这个解决方法。当用户点击单元格时,我将其动画(平移)到左侧以暂时显示“刷我”按钮。这很快就会逆转,因此细胞恢复正常。这提供了一个视觉提示,让用户知道他们可以刷单元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    __block UILabel *swipeLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width,
                                                                   0,
                                                                   200,
                                                                   cell.bounds.size.height)];

    swipeLabel.text = @"  Swipe Me";
    swipeLabel.backgroundColor = [UIColor greenColor];
    swipeLabel.textColor = [UIColor whiteColor];
    [cell addSubview:swipeLabel];

    [UIView animateWithDuration:0.3 animations:^{
        [cell setFrame:CGRectMake(cell.frame.origin.x - 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            [cell setFrame:CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height)];
        } completion:^(BOOL finished) {
            [swipeLabel removeFromSuperview];
            swipeLabel = nil;
        }];
    }];
}

希望这有助于某人。

请注意,您需要将tableViewCell的选择类型设置为none。否则灰色条会遮挡它。

更新。我以为我会发布更多Swifty版本:

func previewActions(forCellAt indexPath: IndexPath) {
    guard let cell = tableView.cellForRow(at: indexPath) else {
        return
    }

    let label: UILabel = {
        let label = UILabel(frame: CGRect.zero)
        label.text = "  Swipe Me  "
        label.backgroundColor = .blue
        label.textColor = .white
        return label
    }()

    // Figure out the best width and update label.frame
    let bestSize = label.sizeThatFits(label.frame.size)
    label.frame = CGRect(x: cell.bounds.width - bestSize.width, y: 0, width: bestSize.width, height: cell.bounds.height)
    cell.insertSubview(label, belowSubview: cell.contentView)

    UIView.animate(withDuration: 0.3, animations: {
        cell.transform = CGAffineTransform.identity.translatedBy(x: -label.bounds.width, y: 0)
        label.transform = CGAffineTransform.identity.translatedBy(x: label.bounds.width, y: 0)
    }) { (finished) in
        UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: {
            cell.transform = CGAffineTransform.identity
            label.transform = CGAffineTransform.identity
        }, completion: { (finished) in
            label.removeFromSuperview()
        })
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    previewActions(forCellAt: indexPath)
    return
}

7
投票

对于任何寻找qaporxswpoi版本的VaporwareWolf的答案的人来说,这里是:

Swift

1
投票

对于Swift 3版本,您希望为每一行调用此函数。

func animateRevealHideActionForRow(tableView: UITableView, indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)

    // Should be used in a block
    var swipeLabel: UILabel? = UILabel.init(frame: CGRect(x: cell!.bounds.size.width,
                                                          y: 0,
                                                          width: 200,
                                                          height: cell!.bounds.size.height))

    swipeLabel!.text = "  Swipe Me";
    swipeLabel!.backgroundColor = UIColor.init(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red
    swipeLabel!.textColor = UIColor.white
    cell!.addSubview(swipeLabel!)

    UIView.animate(withDuration: 0.3, animations: {
        cell!.frame = CGRect(x: cell!.frame.origin.x - 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 100, height: cell!.bounds.size.height)
    }) { (finished) in
        UIView.animate(withDuration: 0.3, animations: {

            cell!.frame = CGRect(x: cell!.frame.origin.x + 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 100, height: cell!.bounds.size.height)

        }, completion: { (finished) in
            swipeLabel?.removeFromSuperview()
            swipeLabel = nil;
        })
    }
}

1
投票

VaporwareWolf的另一个修改版本的答案,这个版本显示了两个矩形而不是一个(与iOS 11的UISwipeActionsConfiguration API很好地配合)。我正在使用这个用于具有两个标签的单元格,这些标签使用约束进行布局,并且只能在设置约束而不是实际单元格或标签时使其正常工作。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath);
    UIView.animate(withDuration: 0.3, animations: {

        cell!.frame = CGRect(x: cell!.frame.origin.x - 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 100, height: cell!.bounds.size.height)

    }) { (finished) in
        UIView.animate(withDuration: 0.3, animations: {

            cell!.frame = CGRect(x: cell!.frame.origin.x + 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 100, height: cell!.bounds.size.height)

        }, completion: { (finished) in
        })
    }
}

0
投票

Swift 3等同于Burak的答案。你可以通过编程方式在任何你想要的地方启动这个调用,我把它放在一个帮助函数中,所以任何表视图都可以显示这个。当用户第一次使用我的应用程序时,我将其刷开(我觉得需要更长的动画时间)。

func showActions(forRow row:Int) {
    guard let cell = (tableView.cellForRow(at: IndexPath(row: row, section: 0))) as? RequestTableViewCell else {
        return
    }

    let labelWidth:CGFloat = 20

    let createLabel:(UIColor)->UILabel = {
        color in
        let label = UILabel(frame: CGRect.zero)
        label.backgroundColor = color
        label.clipsToBounds = false
        label.frame = CGRect(x: cell.bounds.width, y: 0, width: labelWidth, height: cell.bounds.height)
        return label
    }

    let greenLabel = createLabel(.green)
    let redLabel = createLabel(.red)

    //ordering of the subviews is key to get it to look right
    cell.insertSubview(greenLabel, aboveSubview: cell.contentView)
    cell.insertSubview(redLabel, belowSubview: greenLabel)

    let originalLabelSeparationContstraint = cell.labelSeparationContstraint

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 3, options: .curveEaseOut, animations: {
        cell.leftLabelLeadingConstraint.constant -= (labelWidth * 2)
        cell.labelSeparationContstraint.constant = cell.requestAgeLabel.frame.minX - cell.nameLabel.frame.maxX
        cell.rightLabelTrailingContstraint.constant += (labelWidth * 2)
        cell.layoutIfNeeded()

        greenLabel.transform = greenLabel.transform.translatedBy(x: -(labelWidth), y: 0)
        redLabel.transform = redLabel.transform.translatedBy(x: -(labelWidth * 2), y: 0)
    }, completion: {
        _ in
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 3, options: .curveEaseOut, animations: {
            cell.leftLabelLeadingConstraint.constant += (labelWidth * 2)
            cell.labelSeparationContstraint = originalLabelSeparationContstraint
            cell.rightLabelTrailingContstraint.constant -= (labelWidth * 2)
            cell.layoutIfNeeded()

            greenLabel.transform = CGAffineTransform.identity
            redLabel.transform = CGAffineTransform.identity
        }, completion: {
            _ in
            greenLabel.removeFromSuperview()
            redLabel.removeFromSuperview()
        })
    })
}

-1
投票

在实现该功能之前,不要采用hacky方式并使用这样的库。

class func animateRevealHideActionForRow(tableView: UITableView, indexPath: NSIndexPath) { let cell = tableView.cellForRow(at: indexPath as IndexPath); // Should be used in a block var swipeLabel: UILabel? = UILabel.init(frame: CGRect.init(x: cell!.bounds.size.width, y: 0, width: 200, height: cell!.bounds.size.height)) swipeLabel!.text = " Swipe Me"; swipeLabel!.backgroundColor = UIColor.red swipeLabel!.textColor = UIColor.white cell!.addSubview(swipeLabel!) UIView.animate(withDuration: 1.0, animations: { cell!.frame = CGRect.init(x: cell!.frame.origin.x - 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 100, height: cell!.bounds.size.height) }) { (finished) in UIView.animate(withDuration: 1.0, animations: { cell!.frame = CGRect.init(x: cell!.frame.origin.x + 100, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 100, height: cell!.bounds.size.height) }, completion: { (finished) in swipeLabel?.removeFromSuperview() swipeLabel = nil; }) } }

这个库让你可以这样做:https://github.com/SwipeCellKit/SwipeCellKit

© www.soinside.com 2019 - 2024. All rights reserved.