我有一个UITableView,用户可以向左滑动以显示动作(如iOS 8邮件).这一切都按预期工作.当用户点击单元格的某个部分时,我想触发这个.如何以编程方式调用此幻灯片动作?
当前行为:用户必须向左滑动单元才能公开操作按钮.
所需行为:用户在单元格上点击(操作按钮).单元格滑过以公开操作按钮.
解决方法
那么我找不到一个方法来编程,但是我想出了这个解决方法.当用户点击单元格时,我将鼠标左键动画(平移),立即显示“滑动我”按钮.这很快就颠倒了,所以细胞恢复正常.这提供了一个视觉提示,让用户知道他们可以滑动单元格:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; __block UILabel *swipeLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.bounds.size.width,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.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 }