三指捏手势 [英] Three finger pinch gesture

查看:259
本文介绍了三指捏手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试实现三指捏时遇到一些问题.

I'm having some issues trying to implement a 3 finger pinch.

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

I've been using a 2 finger pinch with a 2 finger rotation, individually ! (no simultaneous gesture needed or wanted) the problem is that many times, the system identify the wrong movement because both of them are very similar, so I end up having to remove my fingers and press again in order to try to make the system identify the rotation (usually it identifies the pinch first)

我进行了很多搜索,以查看delayBegin是否有帮助,或者我是否可以做一些动作来激活同时手势,但都无法正常工作,所以我的想法是要用2根手指而不是2根手指来捏(因为捏起来比旋转起来容易).

I searched a lot to see if the delayBegin would help, or if I could do something activating the simultaneous gesture, but none worked ok, so my idea was to instead of using 2 fingers to pinch, I could use 3 (since it's easier to pinch than rotate).

问题是,众所周知,Pinch仅能用2根手指工作.因此,我决定可以对UIPinchGestureReconizer进行子类化,并且仅在屏幕上有3个手指时才允许它工作.其余的操作都可以按标准的捏合方式进行,甚至可以忽略无名指(以计算比例),但要确保无名指仍在屏幕上.

The problem is, as you know, Pinch only works with 2 fingers. So I decided I could subclass the UIPinchGestureReconizer and only allow it to work when there is 3 fingers on screen. The rest it could work as the standard pinch works, even ignoring the third finger (to calculate the scale) but being sure the third finger still on screen.

因此,我尝试了ThreeFingerPinchRecognizer的以下实现(子类为UIPinchGestureRecognizer)

So I tried the following implementation for my ThreeFingerPinchRecognizer (that Sub classes the 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];
 }

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

So, as you can see, I'm trying to get the same functionality of the 2 finger pinch (by only calling the [super] functions, and at the touchesBegan and touchesMoved functions, I'm testing if there is 3 fingers on screen (by looking at the event.alltouches.count)

通过这种方式,两只手指可以完美地旋转,但是捏不能很好地工作,很难激活它,当它捏住时,它就不能像两只手指一样捏住...

With this, the rotation is working perfect with the two fingers, but the pinch is not working very well, its hard to activate it and when it does, it does not work as the two finger pinch...

我知道我做错了这件事,所以任何帮助都会很棒!

I know I may be doing this totally wrong, so any help will be great !!

非常感谢!

推荐答案

请参阅以下代码段,以帮助您识别捏合状态:

See this Snippet can help u identify the State of pinch:

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

这篇关于三指捏手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆