我知道 UIRotateGestureRecognizer 已经是 iOS 的一部分。但这个手势需要两根手指。如何实现只需要一根手指的类似手势识别器?AppStore 中有一款游戏 - Gyrotate 很好地实现了这一点。任何线索表示赞赏。谢谢。
问问题
6387 次
5 回答
6
Kirby Turner 有一个完整的单指旋转手势识别器。
于 2011-10-06T16:09:51.900 回答
5
这是代码 - 它适用于我的模拟器。马克回答说,如果那是你要找的。
// On new touch, start a new array of points
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
// Add each point to the array
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
// At the end of touches, determine whether a circle was drawn
- (void) touchesEnded:(NSSet *) touches withEvent:(UIEvent *) event
{
if (!self.points) return;
if (self.points.count < 3) return;
// Test 1: The start and end points must be between 60 pixels of each other
CGRect tcircle;
if (distance(POINT(0), POINT(self.points.count - 1)) < 60.0f)
tcircle = [self centeredRectangle];
// Test 2: Count the distance traveled in degrees. Must fall within 45 degrees of 2 PI
CGPoint center = CGPointMake(CGRectGetMidX(tcircle), CGRectGetMidY(tcircle));
float distance = ABS(acos(dotproduct(centerPoint(POINT(0), center), centerPoint(POINT(1), center))));
for (int i = 1; i < (self.points.count - 1); i++)
distance += ABS(acos(dotproduct(centerPoint(POINT(i), center), centerPoint(POINT(i+1), center))));
if ((ABS(distance - 2 * M_PI) < (M_PI / 4.0f))) circle = tcircle;
[self setNeedsDisplay];
}
于 2010-08-27T03:13:12.317 回答
3
我使用OneFingerRotation、Scale、Resize和Close功能实现了IQStickerView 。
特点:-
1) 一指旋转刻度。
2) 一指调整大小。
3) 使用属性启用/禁用旋转、缩放、调整大小。
4) 自动管理多个 IQStickerView。
5) 也可以与 UIScrollView 一起使用。
6) 快速响应。
github 存储库在这里:- https://github.com/hackiftekhar/IQStickerView
于 2013-08-19T15:49:58.963 回答
2
尝试探索UIGestureRecognizer类。您应该可以使用自定义它UIGestureRecognizerDelegate
于 2010-08-27T02:05:01.457 回答
1
使用平移手势识别器实现,这是使用手势识别器附加到的另一个 UIView,但它应该与附加到要旋转的视图一起使用。
- (void) gestureRotateButtonPan:(UIPanGestureRecognizer*) gestureRecognizer {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStatePossible:
break;
case UIGestureRecognizerStateBegan:
{
CGPoint location = [gestureRecognizer translationInView:[gestureRecognizer view]];
_touchRotateStartPoint = [self convertPoint:location fromView:[gestureRecognizer view]];
}
break;
case UIGestureRecognizerStateChanged:
{
CGPoint imageLocation = CGPointMake(self.mainImageView.transform.tx + self.mainImageView.center.x
, self.mainImageView.transform.ty + self.mainImageView.center.y);
CGPoint buttonLocation = [gestureRecognizer translationInView:self];
buttonLocation.x += _touchRotateStartPoint.x;
buttonLocation.y += _touchRotateStartPoint.y;
CGFloat currentRotation = atan2(buttonLocation.y - imageLocation.y, buttonLocation.x - imageLocation.x)
- atan2(_touchRotateStartPoint.y - imageLocation.y, _touchRotateStartPoint.x - imageLocation.x);
CGFloat rotation = -(_lastRotation - currentRotation);
CGAffineTransform currentTransform = self.mainImageView.transform;
CGAffineTransform rotatedTransform = CGAffineTransformRotate(currentTransform, rotation);
self.mainImageView.transform = rotatedTransform;
[self setNeedsDisplay];
}
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
_lastRotation = 0.0;
break;
case UIGestureRecognizerStateFailed:
break;
default:
break;
}
}
于 2015-06-11T13:20:27.653 回答