Pla*_*reu 6 uigesturerecognizer ios uipinchgesturerecognizer
我在尝试实施3指捏时遇到了一些问题.
我一直在使用2指捏两指旋转,单独!(没有需要或想要的同时手势)问题是,很多时候,系统识别错误的运动,因为它们都非常相似,所以我最终不得不移开我的手指并再次按下以试图使系统识别旋转(通常首先识别捏)
我搜索了很多,看看它是否delayBegin会有所帮助,或者我是否可以做一些激活同步手势的事情,但没有一个工作正常,所以我的想法是不使用2个手指捏,我可以使用3(因为它更容易捏比旋转).
问题是,如你所知,Pinch只适用于两根手指.所以我决定我可以继承它UIPinchGestureReconizer并且只允许它在屏幕上有3个手指时工作.其余它可以作为标准捏工作,甚至忽略第三个手指(计算比例),但确保第三个手指仍在屏幕上.
所以我尝试了以下的实现ThreeFingerPinchRecognizer(那个Sub类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];
}
Run Code Online (Sandbox Code Playgroud)
所以,正如你所看到的,我正试图获得2指捏的相同功能(仅通过调用[super]函数,在touchesBegan和touchesMoved函数,我正在测试屏幕上是否有3个手指(通过查看event.alltouches.count)
有了这个,两个手指的旋转工作完美,但是捏不能很好,很难激活它,当它发生时,它不能像两指一样捏...
我知道我可能完全错了,所以任何帮助都会很棒!
非常感谢你!
小智 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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1657 次 |
| 最近记录: |