三指捏手势

时间:2012-12-21 00:01:50

标签: ios uigesturerecognizer uipinchgesturerecognizer

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

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

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

问题是,如你所知,Pinch只适用于两根手指。所以我决定我可以继承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]函数,以及touchesBegantouchesMoved功能,我正在测试屏幕上是否有3个手指(通过查看event.alltouches.count

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

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

非常感谢!

1 个答案:

答案 0 :(得分: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;
    }
}