【发布时间】:2013-10-29 15:43:11
【问题描述】:
我正在尝试创建一个能够检测 4 个手指旋转的手势识别器(旋转音量旋钮时类似)。
主要思想是创建 UIRotateGestureRecognizer 的子类并覆盖其方法。在-touchesBegan 中,我检测了触摸次数,如果次数低于 4,则手势状态为失败。
之后,我将位置点传递给查找凸包直径的算法。如果您考虑一下,您的手指就是顶点,我只需要找到最大距离的两个顶点。获得这两个点后,我将它们作为 ivar 引用,并将它们传递给超类,因为它只是用两根手指进行的简单旋转。
它不起作用:
- 触摸检测似乎很难
-
-touchesHasMoved很少被调用 - 当它被调用时,它大部分时间都挂起
有人可以帮我吗?
代码如下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (touches.count<4) {
//FAIL
self.state = UIGestureRecognizerStateFailed;
return;
}
//Find the diameter of the convex hull
NSArray * touchesArray = [touches allObjects];
NSMutableArray * pointsArray = @[].mutableCopy;
for (UITouch * touch in touchesArray) {
[pointsArray addObject:[NSValue valueWithCGPoint:[touch locationInView:touch.view]]];
}
DiameterType convexHullDiameter = getDiameterFromPoints(pointsArray);
CGPoint firstPoint = convexHullDiameter.firstPoint;
CGPoint secondPoint = convexHullDiameter.secondPoint;
for (UITouch * touch in touchesArray) {
if (CGPointEqualToPoint([touch locationInView:touch.view], firstPoint) ) {
self.fistTouch = touch;
}
else if (CGPointEqualToPoint([touch locationInView:touch.view], secondPoint)){
self.secondTouch = touch;
}
}
//Calculating the rotation center as a mid point between the diameter vertices
CGPoint rotationCenter = (CGPoint) {
.x = (convexHullDiameter.firstPoint.x + convexHullDiameter.secondPoint.x)/2,
.y = (convexHullDiameter.firstPoint.y + convexHullDiameter.secondPoint.y)/2
};
self.rotationCenter = rotationCenter;
//Passing touches to super as a fake rotation gesture
NSSet * touchesSet = [[NSSet alloc] initWithObjects:self.fistTouch, self.secondTouch, nil];
[super touchesBegan:touchesSet withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (touches.count<4) {
self.state = UIGestureRecognizerStateFailed;
return;
}
[super touchesMoved:[[NSSet alloc] initWithObjects:self.fistTouch, self.secondTouch, nil] withEvent:event];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:[[NSSet alloc] initWithObjects:self.fistTouch, self.secondTouch, nil] withEvent:event];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:[[NSSet alloc] initWithObjects:self.fistTouch, self.secondTouch, nil] withEvent:event];
}
【问题讨论】:
-
你不能只使用 2 根手指并使用旋转/俯仰手势(默认来自 ios)吗?我这么说是因为我用两个手指来改变音响系统的音量,而不是 4 个。可能我会用更多的手指来处理重物,比如硬自来水,p.ex.
-
如果我只用两根手指,从用户的角度来看就不一样了。
-
也许使用“[[event allTouches] anyObject];”而不是“[touches allObjects];”有点帮助,我不明白你的问题到底是什么。
标签: ios objective-c rotation uigesturerecognizer