我認爲你是對的,至少,這就是我對我的細胞所做的。
唯一的區別是我沒有使寬度比窗口寬度大。我添加了額外的東西,在我的情況下,它是一個刪除按鈕,在常規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;
}];
}
}
那是我的代碼,可能無法正常工作完全像你想要的,但我希望,給了你關於如何做到這一點
執行在github以下搜索一個粗略的想法 - 第幾命中是工作臺在您滑動時提供額外選項的單元格電子郵件:https://github.com/search?q=uitableviewcell&ref=cmdform – rmaddy