我有一个UITableView,用户可以向左滑动以显示操作(如在iOS 8邮件中)。这一切都按预期工作。我想在用户点击单元格的某个部分时触发此操作。如何以编程方式调用此幻灯片操作?
当前行为:用户必须向左滑动单元格才能显示操作按钮。
所需行为:用户点击单元格上的(操作按钮)。单元格滑过以显示操作按钮。
答案 0 :(得分:15)
好吧,我无法找到以编程方式执行此操作的方法,但我想出了这个解决方法。当用户点击单元格时,我将其动画(平移)到左侧以暂时显示出来并且#34; Swipe Me"按钮。这很快就会逆转,因此细胞恢复正常。这提供了一个视觉提示,让用户知道他们可以刷小区:
- (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
}
答案 1 :(得分:7)
对于任何搜索VaporwareWolf答案Swift
版本的人来说,这里是:
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;
})
}
}
答案 2 :(得分:1)
对于Swift 3版本,您希望为每一行调用此函数。
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
})
}
}
答案 3 :(得分:1)
VaporwareWolf答案的另一个修改版本,这个版本显示了两个矩形而不是一个(与iOS 11的UISwipeActionsConfiguration API很好地配合)。我正在使用这个用于具有两个标签的单元格,这些标签具有约束条件,并且只能在设置约束而不是实际单元格或标签时才能使其正常工作。
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()
})
})
}
答案 4 :(得分:0)
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;
})
}
}
答案 5 :(得分:-1)
在实现该功能之前,请不要随意使用此类库。
https://github.com/SwipeCellKit/SwipeCellKit
该库使您可以执行以下操作:cell.showSwipe(orientation: .right, animated: true)