6

我在尝试实现 3 指捏合时遇到了一些问题。

我一直在单独使用 2 指捏和 2 指旋转!(不需要或想要同时手势)问题是很多时候,系统识别出错误的动作,因为它们非常相似,所以我最终不得不移开手指并再次按下以尝试让系统识别旋转(通常它首先识别夹点)

我进行了很多搜索以查看是否有delayBegin帮助,或者我是否可以做一些激活同时手势的操作,但没有一个可以正常工作,所以我的想法是不要使用 2 个手指捏,我可以使用 3 个(因为它更容易捏比旋转)。

问题是,如您所知,Pinch 仅适用于 2 个手指。所以我决定我可以子类化UIPinchGestureReconizer并只允许它在屏幕上有 3 个手指时工作。其余的它可以像标准捏合一样工作,即使忽略无名指(计算比例)但确保无名指仍在屏幕上。

所以我为我的ThreeFingerPinchRecognizer(那个子类UIPinchGestureRecognizer)尝试了以下实现

@implementation GRThreeFingerPinchRecognizer

-(id)initWithTarget:(id)target action:(SEL)action
    {
    self = [super initWithTarget:target action:action];
    if(self){
    }
    return self;
}

- (void)reset
{
    [super reset];
}

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    int numberOfTouches = event.allTouches.count;
    if (numberOfTouches == 3) 
     {
        [super touchesBegan:touches withEvent:event];
     }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    int numberOfTouches = event.allTouches.count;
    if (numberOfTouches == 3)
    {
        [super touchesMoved:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ 
        [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
 }

因此,如您所见,我正在尝试获得与 2 指捏持相同的功能(通过仅调用[super]函数,并且在touchesBeganandtouchesMoved函数中,我正在测试屏幕上是否有 3 个手指(通过查看event.alltouches.count)

有了这个,两根手指的旋转效果很好,但是捏的效果不是很好,很难激活它,当它激活时,它不能像两指捏一样工作......

我知道我可能做错了,所以任何帮助都会很棒!

非常感谢!

4

1 回答 1

0

看到这个片段可以帮助你识别捏的状态:

if (pinch.numberOfTouches > 1)
{
    CGPoint firstPoint = [pinch locationOfTouch:0 inView:self];
    CGPoint secPoint = [pinch locationOfTouch:1 inView:self];
    currentUpperY = MIN(firstPoint.y, secPoint.y);
    if (previousY == 0) previousY = currentUpperY;
    Float32 y = (self.contentOffset.y + previousY - currentUpperY);
    [self setContentOffset:CGPointMake(0, y < 0 ? 0 : y) animated:NO];

    if (pinch.state == UIGestureRecognizerStateBegan)
    {
        pinchStarted = YES;
        firstY = MIN(firstPoint.y, secPoint.y);
        secondY = MAX(firstPoint.y, secPoint.y);
        NSArray *pinchedIndexs = [self indexPathsForRowsInRect:CGRectMake(0.0, firstY, CGRectGetWidth(self.bounds), secondY)];
        if (pinchedIndexs.count) itemToOpenOrClose = [[currentItems subarrayWithRange:NSMakeRange(((NSIndexPath *)[pinchedIndexs objectAtIndex:0]).row, pinchedIndexs.count - 1)] copy];
    }
}

if ((pinch.state == UIGestureRecognizerStateChanged && pinchStarted && itemToOpenOrClose.count)
    || pinch.state == UIGestureRecognizerStateEnded)
{
    if (pinch.scale > 1) // Pinch OUT
    {
        for (Item *item in itemToOpenOrClose)
        {                
            [self openItem:item inIndexPath:[NSIndexPath indexPathForRow:[currentItems indexOfObject:item] inSection:0]];
        }
    }
    else if (pinch.scale < 1) // Pinch IN
    {
        for (Item *item in itemToOpenOrClose)
        {
            [self closeItem:item inIndexPath:[NSIndexPath indexPathForRow:[currentItems indexOfObject:item] inSection:0]];
        }
    }

    if (pinch.state == UIGestureRecognizerStateEnded)
    {
        pinchStarted = NO;
        itemToOpenOrClose = nil;
        previousY = 0;
    }
}
于 2013-03-07T07:49:29.290 回答