2014-01-24 23 views
2

我正在嘗試爲我創建的應用程序實施「額外」菜單。基本上,我有一個UITableView與多種類型的單元格,當用戶滑動到單元格的左側時,我希望能夠動態顯示他們可以做的「額外」。例如,在一篇文章中,他們可以輕掃,然後看到諸如share等選項。如果你已經在Reddit上使用ios上的外星人藍色應用程序,那麼這就是我期望做的...UITableView子視圖時滑動以顯示「額外」

到目前爲止,我有刷卡識別器的工作,它確定細胞的類型正確...我只是「知道了如何啓動子視圖編程...

難道我只是讓每一個細胞較大,隱藏額外直到刷卡還是我動態添加視圖到每個細胞,我去......

感謝任何建議或幫助

我可以提供代碼,如果需要....

A

+0

執行在github以下搜索一個粗略的想法 - 第幾命中是工作臺在您滑動時提供額外選項的單元格電子郵件:https://github.com/search?q=uitableviewcell&ref=cmdform – rmaddy

回答

3

我認爲你是對的,至少,這就是我對我的細胞所做的。

唯一的區別是我沒有使寬度比窗口寬度大。我添加了額外的東西,在我的情況下,它是一個刪除按鈕,在常規cell.contentview下。那麼當單元檢測到從右向左滑動時,它將調用一個函數來處理該手勢。

因爲我希望用戶看到刪除按鈕時拖動單元格的左側,並顯示該按鈕完全當他們平移細胞遠遠不夠

這裏是我如何處理平移手勢片段,

CGPoint _originalTouchPoint; 

- (void)handlePan:(UIPanGestureRecognizer *)recognizer 
{ 
    switch (recognizer.state) { 
     case UIGestureRecognizerStateBegan: 
      //save the original touch point when pan starts 
      _originalTouchPoint = [recognizer locationInView:_tableView]; 
      break; 

     case UIGestureRecognizerStateChanged: 
      [self handlePan:recognizer]; 
      break; 

     case UIGestureRecognizerStateEnded: 
      [self panEnded:recognizer]; 
      break; 
     default: 
      break; 
    } 
} 

- (void)handlePan:(UIPanGestureRecognizer *)recognizer 
{ 
    MyCell *ourCell = (MyCell *)recognizer.view; 

    CGPoint touchPoint = [recognizer locationInView:_tableView]; 
    float movedDistance = (_originalTouchPoint.x - touchPoint.x); 

    if(movedDistance < 0) 
     movedDistance = 0; 

    float maxX = ourCell.deleteButton.frame.size.width; 

    float ourTranslation = MAX(-1 * ourCell.deleteButton.frame.size.width, -1 * movedDistance); 
    ourCell.contentView.transform = CGAffineTransformMakeTranslation(ourTranslation, 0.0f); 

    // i only show the button when user pan all the way though 
    _shouldDeleteButtonShow = movedDistance/maxX >= 1.0; 
} 

- (void)panEnded:(UIPanGestureRecognizer *)recognizer 
{ 
    MyCell *ourCell = (MyCell *)recognizer.view; 

    if (_shouldDeleteButtonShow) 
    { 
     //do whatever you want in this case 
    } 
    else 
    { 
     //move the cell back to normal 
     [UIView animateWithDuration:0.3 
         animations:^ 
     { 
      ourCell.contentView.transform = CGAffineTransformIdentity; 
     }]; 
    } 

} 

那是我的代碼,可能無法正常工作完全像你想要的,但我希望,給了你關於如何做到這一點

+0

感謝您的幫助 - 我想通過合併您所說的內容來達到目的:接受 – Andy

相關問題