我无法滚动查看滚动视图(在表格视图中)。基本上,我有一个if语句,用于检查水平移动是否大于垂直移动。如果是,则在单元格上执行平移手势。另外,我希望tableview能够正常滚动。我尝试使用self.scrollview.enabled = yes
的一些变体,但我无法使其正常工作。
它适用于水平平移手势,但我不能让它在else部分正确滚动。这是最相关的代码:(对不起,如果它很糟糕 - 我还是iOS / Objective C的新手)。哦,如果你在代码提取中随机看到一个奇怪的'代码',请忽略它 - 我在格式化时遇到了一些问题而且我丢了一个字。
-(void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{
CGPoint location = [panGestureRecognizer locationInView:_tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
TVTableCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
CGPoint translation = [panGestureRecognizer translationInView:cell];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y)) {
CGPoint originalCenter = cell.center;
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"pan gesture started");
}
if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
// translate the center
CGPoint translation = [panGestureRecognizer translationInView:self.view];
if (translation.x > 0) {
cell.center = CGPointMake(originalCenter.x + (translation.x-(translation.x-3)), originalCenter.y);
} else {
cell.center = CGPointMake(originalCenter.x + (translation.x-(translation.x+3)), originalCenter.y);
}
// determine whether the item has been dragged far enough to initiate a delete / complete
//this will be implemented eventually
// _deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width / 2;
NSLog(@"state changed");
}
if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
// the frame this cell would have had before being dragged
CGRect originalFrame = CGRectMake(0, cell.frame.origin.y,
cell.bounds.size.width, cell.bounds.size.height);
// if (!_deleteOnDragRelease) {
// if the item is not being deleted, snap back to the original location
[UIView animateWithDuration:0.2
animations:^{
cell.frame = originalFrame;
}
];
}
} else {
//else: scroll tableview normally
NSLog(@"dear god act normally");
}
}
感谢您的帮助,非常欢迎所有建议。
答案 0 :(得分:8)
我真的不喜欢UITableView
,但我想问题是将自定义UIPanGestureRecognizer
分配给_tableView
,您基本上会使默认值无效。无论如何,你可以用这种方式解决它。
让我们假设您在ViewController中执行所有操作,即使它非常脏。
使ViewController符合UIGestureRecognizerDelegate
protocol
在ViewController.m
覆盖gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
方法。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
正如文件所说:
询问代表是否应允许两个手势识别器同时识别手势。
返回值
是,允许gestureRecognizer和otherGestureRecognizer同时识别他们的手势。默认实现返回NO - 不能同时识别两个手势。
这样你的pan识别器和默认的UITableView
都会运行。
您需要做的最后一件事是将ViewController设置为UIPanGestureRecognizer
的委托:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.delegate = self;
注意:这是您可以实施的最重要且最脏的解决方案。更好的解决方案是将手势跟踪逻辑转换为单元格本身,或者将UIPanGestureRecognizer
子类化。也请看this answer。