三指捏手势



我遇到了一些试图实现3指捏的问题。

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

我经常搜索delayBegin是否会有所帮助,或者我是否可以执行同时手势的事情,但是没有任何事情都可以,所以我的想法是使用2个手指捏住,我可以使用3(因为比旋转更容易捏)。

您知道的是,捏合只用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]函数,并且在touchesBegantouchesMoved功能下,我正在测试是否有3个手指屏幕(通过查看event.alltouches.count

这样,旋转与两个手指完美地工作,但是捏合不太好,很难激活它,当它这样做时,它不起作用,因为两个手指捏...

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

非常感谢!

请参阅此片段可以帮助您识别捏的状态:

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;
    }
}

最新更新