想知道以下是否可以处理UIGestureRecognizer触摸:
if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
{
//process touches
}
else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
//do whatever after gesture recognizer finishes
}
基本上,我想知道UIGestureRecognizerStateEnded
是否会在UIGestureRecognizer
仍处理过程中发生{或1}},或者在此时是否已完成所有触摸?我为translationInView
获得了奇怪的价值,所以我想在这里问一下。
答案 0 :(得分:1)
你问:
基本上,我想知道
UIGestureRecognizerStateEnded
是否会在UIGestureRecognizer
仍处理过程中发生{或1}},或者在此时是否已完成所有触摸?
当你得到UIGestureRecognizerStateEnded
时,是的,手势就完成了。但显然,除非您在该点从视图中删除手势识别器,否则如果用户开始新手势,则手势识别过程将从UIGestureRecognizerStateBegan
开始重新开始。
此外,你说:
我为
translationInView
获得了奇怪的价值,所以我想在这里问一下。
你的代码示例表明你正在处理一个没有translationInView
的捏手势,所以我不确定你得到了什么“奇怪的值”。您可以通过设置手势delegate
并实施shouldRecognizeSimultaneouslyWithGestureRecognizer
来同时进行两次手势:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePinch:)];
pinch.delegate = self;
[self.view addGestureRecognizer:pinch];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]])
return YES;
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
return YES;
return NO;
}
- (void)handlePinch:(UIPinchGestureRecognizer *)gesture
{
CGFloat scale = [gesture scale];
NSLog(@"%s: %@: scale=%.2f", __FUNCTION__, [self stringFromGestureState:gesture.state], scale);
}
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
CGPoint translation = [gesture translationInView:gesture.view];
NSLog(@"%s: %@: translation=%@", __FUNCTION__, [self stringFromGestureState:gesture.state], NSStringFromCGPoint(translation));
}
以上代码有效,handlePan
返回平移,而handlePinch
返回捏,translationInView
的{{1}}看起来无异常。也许你可以向我们展示你是如何使用捏合手势并获得handlePan
并告诉我们你所获得的价值中有多奇怪。