我想在我的UITableViewController中實現一個UIPanGestureRecognizer以用於滑動刪除動畫。與清除應用程序中使用的滑動刪除類似,如果您在左側或右側滑動UITableViewCell,則單元格移動並被刪除。在UITableViewCell中使用UIPanGestureRecognizer的問題
我已經嘗試在我的UITableViewCell子類中實現這一點,但它似乎永遠不會收到事件。
這是我放在我的UITableViewCell子類嘗試此功能的代碼。在我的init方法
UIGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
,然後方法來處理它:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:self.superview];
//might have to change view to tableView
//check for the horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
NSLog(@"Panning");
}
return NO;
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
//if the gesture has just started record the center location
NSLog(@"handlePan");
_originalCenter = self.center; //Declared as a CGPoint at the top of my TableViewCell
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
//translate the center (aka translate from the center of the cell)
CGPoint translation = [recognizer translationInView:self];
self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
// determine whether the item has been dragged far enough to delete/complete
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
CGRect originalFrame = CGRectMake(0, self.frame.origin.y, self.bounds.origin.x, self.bounds.size.height);
[UIView animateWithDuration:0.2 animations:^{
self.frame = originalFrame;}
];
}
}
細胞完全不動,雖然。不太確定這裏發生了什麼
這會得到執行嗎?的NSLog(@ 「handlePan」); – 2013-03-12 20:51:09
還將NSLog(@「panning」)移動到return語句上方,以便輸出。 – 2013-03-12 20:51:54
這兩個日誌都不會執行。我不確定發生了什麼事。這些單元格都是完全固定的 – cherbear 2013-03-12 21:21:28