如何添加将调用的手势(从左到右)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete)
{
//
}
}
在 iOS7 中通过子类化 UITableViewCell 的方法。
最佳答案
您应该创建从 UITableViewCell 传递的自定义类,并且您应该在 init 中添加 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 添加协议(protocol):并且需要在 tableView:commitEditingStyle 中实现它。如果您需要更多帮助,请告诉我。 希望这有帮助
关于ios - 继承 UITableViewCell 并在 iOS 中添加自己的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212696/