检测UITableView中的水平平移 [英] Detect horizontal panning in UITableView
问题描述
我使用UIPanGestureRecognizer来识别UITableView中的水平滑动(在单元格上是精确的,虽然它被添加到表本身)。然而,这个手势识别器显然窃取了来自表格的触摸。我已经得到了pangesturerecognizer识别水平滑动,然后捕捉到;但是如果用户通过滑动垂直开始,它应该将所有事件从该触摸传递到tableview。
I'm using a UIPanGestureRecognizer to recognize horizontal sliding in a UITableView (on a cell to be precise, though it is added to the table itself). However, this gesture recognizer obviously steals the touches from the table. I already got the pangesturerecognizer to recognize horizontal sliding and then snap to that; but if the user starts by sliding vertical, it should pass all events from that touch to the tableview.
我试过的一件事是禁用识别器,直到下一个触摸事件。所以我需要它立即通过事件然后。
One thing i have tried was disabling the recognizer, but then it wouldn't scroll untill the next touch event. So i'd need it to pass the event right away then.
我尝试的另一件事是让它滚动自己,但然后你会错过停止后的持续速度
Another thing i tried was making it scroll myself, but then you will miss the persistent speed after stopping the touch.
还有一些代码:
//In the viewdidload method
UIPanGestureRecognizer *slideRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(sliding:)];
[myTable addGestureRecognizer:slideRecognizer];
-(void)sliding:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
CGPoint translation = [recognizer translationInView:favoritesTable];
if (sqrt(translation.x*translation.x)/sqrt(translation.y*translation.y)>1) {
horizontalScrolling = YES; //BOOL declared in the header file
NSLog(@"horizontal");
//And some code to determine what cell is being scrolled:
CGPoint slideLocation = [recognizer locationInView:myTable];
slidingCell = [myTable indexPathForRowAtPoint:slideLocation];
if (slidingCell.row == 0) {
slidingCell = nil;
}
}
else
{
NSLog(@"cancel");
}
if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled)
{
horizontalScrolling = NO;
}
if (horizontalScrolling)
{
//Perform some code
}
else
{
//Maybe pass the touch from here; It's panning vertically
}
}
如何传递触摸?
添加:我也认为可能是tableview的手势识别器方法的子类,首先检查它是否水平;然而,然后,我将需要原始的代码,我想...不知道苹果是否会有它的问题。
另外:我没有继承UITableView(控制器),只是单元格。这个代码在viewcontroller中,它保存表;)
Addition: I also thought to maybe subclass the tableview's gesture recognizer method, to first check if it's horizontal; However, then i would need the original code, i suppose... No idea if Apple will have problems with it. Also: I didn't subclass the UITableView(controller), just the cells. This code is in the viewcontroller which holds the table ;)
推荐答案
我有同样的问题,与UIPanGestureRecognizer。
I had the same issue and came up with a solution that works with the UIPanGestureRecognizer.
与Erik相比,我直接将UIPanGestureRecognizer添加到单元格中,因为我只需要一个特定的单元格来支持平移。但我想这应该适用于Erik的情况。
In contrast to Erik I've added the UIPanGestureRecognizer to the cell directly, as I need just one particular cell at once to support the pan. But I guess this should work for Erik's case as well.
以下是代码。
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *cell = [gestureRecognizer view];
CGPoint translation = [gestureRecognizer translationInView:[cell superview]];
// Check for horizontal gesture
if (fabsf(translation.x) > fabsf(translation.y))
{
return YES;
}
return NO;
}
水平姿势的计算是从Erik的代码复制的 - 这与iOS 4.3。
The calculation for the horizontal gesture is copied form Erik's code – I've tested this with iOS 4.3.
编辑:
我发现这个实现阻止滑动到删除手势。为了恢复这种行为,我添加了检查手势的速度到上面的if语句。
I've found out that this implementation prevents the "swipe-to-delete" gesture. To regain that behavior I've added check for the velocity of the gesture to the if-statement above.
if ([gestureRecognizer velocityInView:cell].x < 600 && sqrt(translate...
我的设备上的一个位置我想出了一个500到600的速度,这在我看来提供了平移和滑动到删除手势之间的过渡的最佳用户体验。
After playing a bit on my device I came up with a velocity of 500 to 600 which offers in my opinion the best user experience for the transition between the pan and the swipe-to-delete gesture.
这篇关于检测UITableView中的水平平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!