我知道UIRotateGestureRecognizer已经是iOS的一部分了。但这个手势需要两个手指。我如何才能实现一个只需要一根手指的类似手势识别器?在AppStore中有一款游戏 - Gyrotate有很好的实现。任何线索都值得赞赏。 THX。
答案 0 :(得分:6)
Kirby Turner有一个完整的单指旋转手势识别器here。
答案 1 :(得分:5)
这是代码 - 它可以在我的模拟器上运行。 Mark回答你是否正在寻找。
// 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];
}
答案 2 :(得分:2)
尝试浏览UIGestureRecognizer课程。您应该可以使用UIGestureRecognizerDelegate
答案 3 :(得分:2)
我使用 OneFingerRotation ,比例,调整大小和关闭功能实施 IQStickerView
功能强> 的: -
1)一个手指旋转刻度。
2)单手调整大小。
3)启用/ Desable旋转,缩放,调整属性大小。
4)自动管理多个IQStickerView。
5)也可以使用UIScrollView。
6)快速响应。
github repositor在这里: - https://github.com/hackiftekhar/IQStickerView
答案 4 :(得分: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;
}
}