2014-11-05 29 views
12

我有一個UITableView,用戶可以向左滑動以顯示操作(如iOS 8郵件)。這一切都按預期工作。我想在用戶點擊單元格的某個部分時觸發此操作。我如何以編程方式調用此幻燈片操作?UITableView以編程方式調用滑動操作

當前行爲:用戶必須向左滑動顯示操作按鈕。

期望的行爲:用戶在單元格上點擊(一個動作按鈕)。單元格滑過以揭示動作按鈕。

+0

你有沒有找到一個程序化的方式來做到這一點,而不是你的解決方法?謝謝 – rwyland 2015-06-02 20:40:06

+0

我沒有。我認爲目前還沒有公開的API。也許iOS 9? – VaporwareWolf 2015-06-03 18:00:41

+0

任何方式來顯示UITableViewRowAction而不用滑動,但它可以programmatelly顯示用戶交互? – 2016-08-18 14:12:04

回答

10

嗯,我無法找到一種方法來做到這一點編程,但我想出了這個解決方法。當用戶點擊單元格時,我將其動畫(平移)到左側以便瞬間顯示一個「點擊我」按鈕。這很快就會逆轉,因此細胞恢復正常。這提供了一個視覺提示,讓用戶知道他們可以刷單元格:

- (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 
} 
+1

太棒了。對於第二部分,我使用的延遲代替文本顯示出來的一瞬間 '[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

+0

太棒了!同時確保「Clip to bounds」被禁用或標籤不可見。 – rstewart22 2018-02-22 12:25:09

4

對任何人來說,那就是:

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; 
     }) 

    } 
} 
0

而對於一個斯威夫特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 
     }) 
    } 
} 
0

斯威夫特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; 
     }) 
    } 
} 
相關問題