我有一个UITableView,用户可以向左滑动以显示操作(如iOS 8邮件)。这一切都按预期工作。我想在用户点击单元格的某个部分时触发此操作。我如何以编程方式调用此幻灯片操作?UITableView以编程方式调用滑动操作
当前行为:用户必须向左滑动显示操作按钮。
期望的行为:用户在单元格上点击(一个动作按钮)。单元格滑过以揭示动作按钮。
我有一个UITableView,用户可以向左滑动以显示操作(如iOS 8邮件)。这一切都按预期工作。我想在用户点击单元格的某个部分时触发此操作。我如何以编程方式调用此幻灯片操作?UITableView以编程方式调用滑动操作
当前行为:用户必须向左滑动显示操作按钮。
期望的行为:用户在单元格上点击(一个动作按钮)。单元格滑过以揭示动作按钮。
嗯,我无法找到一种方法来做到这一点编程,但我想出了这个解决方法。当用户点击单元格时,我将其动画(平移)到左侧以便瞬间显示一个“点击我”按钮。这很快就会逆转,因此细胞恢复正常。这提供了一个视觉提示,让用户知道他们可以刷单元格:
- (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版本:在搜索Swift
版本VaporwareWolf的回答
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
}
太棒了。对于第二部分,我使用的延迟代替文本显示出来的一瞬间 '[UIView的animateWithDuration:0.5 延迟:0.5 选项:0 动画:^ { [细胞SETFRAME:CGRectMake(cell.frame.origin .x + 100,cell.frame.origin.y,cell.bounds.size.width,cell.bounds.size.height)]; } 完成:^(完成BOOL){ [swipeLabel removeFromSuperview]; swipeLabel = nil; }];' – CyberMew 2015-02-13 05:57:51
太棒了!同时确保“Clip to bounds”被禁用或标签不可见。 – rstewart22 2018-02-22 12:25:09
对任何人来说,那就是:
func animateRevealHideActionForRow(tableView: UITableView, indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath);
// Should be used in a block
var swipeLabel: UILabel? = UILabel.init(frame: CGRectMake(cell!.bounds.size.width,
0,
200,
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.whiteColor()
cell!.addSubview(swipeLabel!)
UIView.animateWithDuration(0.3, animations: {
cell!.frame = CGRectMake(cell!.frame.origin.x - 100, cell!.frame.origin.y, cell!.bounds.size.width + 100, cell!.bounds.size.height)
}) { (finished) in
UIView.animateWithDuration(0.3, animations: {
cell!.frame = CGRectMake(cell!.frame.origin.x + 100, cell!.frame.origin.y, cell!.bounds.size.width - 100, cell!.bounds.size.height)
}, completion: { (finished) in
swipeLabel?.removeFromSuperview()
swipeLabel = nil;
})
}
}
而对于一个斯威夫特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相当于布拉克的答案。你可以通过编程的方式将这个调用发送到任何你想要的地方,我把它放在一个辅助函数中,这样任何表视图都可以显示出来。第一次用户使用我的应用程序时,我将其滑开(我感觉需要更长的动画时间)。
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;
})
}
}
你有没有找到一个程序化的方式来做到这一点,而不是你的解决方法?谢谢 – rwyland 2015-06-02 20:40:06
我没有。我认为目前还没有公开的API。也许iOS 9? – VaporwareWolf 2015-06-03 18:00:41
任何方式来显示UITableViewRowAction而不用滑动,但它可以programmatelly显示用户交互? – 2016-08-18 14:12:04