您應該創建自定義類,從的UITableViewCell交付,你應該在初始化添加UIPanGestureRecognizer:
UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
的下一步是替代方法gestureRecognizerShouldBegin:
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:[self superview]];
if (fabsf(translation.x) > fabsf(translation.y)) {
return YES;
}
return NO;
}
,並添加handlePan:
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
_originalCenter = self.center; //variable to keep centre
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self];
//this check out if you drag more than half of the screen width
self.center = CGPointMake(_originalCenter.x + translation.x, _originalCenter.y);
_deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width/2;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGRect originalFrame = CGRectMake(0, self.frame.origin.y,
self.bounds.size.width, self.bounds.size.height);
if (!_deleteOnDragRelease) {
[UIView animateWithDuration:0.2
animations:^{
//this is for animate that you move the cell but you don't need it (it just look cool)
self.frame = originalFrame;
}
];
}
if (_deleteOnDragRelease) {
// need to implement your delegate
[self.delegate itemToDeleted:self.YOURDATA];
}
}
}
您需要使用方法添加協議itemToDeleted:你需要實現它的tableView:commitEditingStyle。讓我知道你是否需要更多的幫助。 希望這個幫助
IOS的默認功能。如果不工作嘗試新的viewcontroller – Vinodh