我在尝试实现三指捏合时遇到了一些问题。
我一直在单独使用 2 指捏和 2 指旋转! (不需要或不需要同时手势)问题是很多时候,系统识别出错误的 Action ,因为它们非常相似,所以我最终不得不移开手指并再次按下以尝试让系统识别旋转(通常它首先识别捏)
我搜索了很多,看看 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]
函数,并在 touchesBegan
和 touchesMoved
函数,我正在测试屏幕上是否有 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;
}
}
关于ios - 三指捏手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982409/