【问题标题】:iOS: Single Finger rotation and translation of an imageiOS:图像的单指旋转和平移
【发布时间】:2011-11-10 18:50:10
【问题描述】:

目前我正在关注此Link 对图像执行单指旋转并且工作正常。但我想知道执行旋转后,如果我想将图像从屏幕的一个位置移动到任何其他职位。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // We can look at any touch object since we know we 
   // have only 1. If there were more than 1 then 
   // touchesBegan:withEvent: would have failed the recognizer.
   UITouch *touch = [touches anyObject];

   // A tap can have slight movement, but we're not interested
   // in a tap. We want more movement. So if a tap is detected
   // fail the recognizer. 
   if ([self state] == UIGestureRecognizerStatePossible) {
      [self setState:UIGestureRecognizerStateBegan];
   } else {
      [self setState:UIGestureRecognizerStateChanged];
   }

   // To rotate with one finger, we simulate a second finger.
   // The second figure is on the opposite side of the virtual
   // circle that represents the rotation gesture.

   UIView *view = [self view];
   CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
   CGPoint currentTouchPoint = [touch locationInView:view];
   CGPoint previousTouchPoint = [touch previousLocationInView:view];

   // use the movement of the touch to decide
   // how much to rotate the carousel
   CGPoint line2Start = currentTouchPoint;
   CGPoint line1Start = previousTouchPoint;
   CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y));
   CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y));

   //////
   // Calculate the angle in radians.
   // (From: http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html )
   CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;

   CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
   CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

    CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

   CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs;
   //////

   [self setRotation:angleInRadians];
}

【问题讨论】:

    标签: iphone ipad uitouch


    【解决方案1】:

    我不确定我的回答是否会对您有所帮助,但我担心您不能同时进行“一键式”旋转和“一键式”翻译。实际上,您的代码总是通过“模拟”第二根手指触摸显示屏的情况将单次触摸解释为旋转。

    您可以尝试区分两个单指手势,以便将一个与旋转相关联,另一个与平移相关联。例如,翻译手势可以是“长按”(即,您触摸并保持触摸一段时间,然后移动对象)。

    无论如何,希望这会有所帮助......

    【讨论】:

    • 但我目前使用的图像尺寸为 30*30,这是项目要求。如果我执行双指旋转,它不会正常发生。所以我能做什么,任何建议或任何指针?
    • 我唯一能出来的就是我说的:你按下(比如说,半秒)然后拖动图像;或者,您滑动以旋转它。看起来相当一致,应该可以很好地处理 30x30 图像...
    • 对于长按你的意思是 UILongPressGestureRecognizer 或者我必须在 UITouch 事件方法中实现它..
    • 为了移动我之前实现的图像 - (void)translate:(UIPanGestureRecognizer *)gesture { if ((gesture.state == UIGestureRecognizerStateChanged) ||(gesture.state == UIGestureRecognizerStateEnded)) { self.transform = CGAffineTransformTranslate(self.transform, translation.x, translation.y); [手势setTranslation:CGPointZero inView:self];但是在拖动我的图像时它超出了边界那么我该如何修复它
    • 我认为通过这样做:self.transform = CGAffineTransformTranslate(self.transform... 您正在累积翻译(可以说,您不断将翻译添加到同一个翻译矩阵)。尝试改用CGAffineTransformMakeTranslation,这样每次你应用一个新的翻译。只是猜测......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2014-06-30
    • 2016-09-22
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多