【问题标题】:UIImageView rotation animation in the touched directionUIImageView 触摸方向旋转动画
【发布时间】:2012-07-11 11:16:19
【问题描述】:

我有一个带有箭头图像的 UIImageView。当用户点击 UIView 时,这个箭头应该指向点击的方向,保持它的位置,它应该只是改变变换。我已经实现了以下代码。但它没有按预期工作。我添加了一个截图。在此屏幕截图中,当我触摸左上角时,箭头方向应如图所示。但事实并非如此。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch=[[event allTouches]anyObject];
 touchedPoint= [touch locationInView:touch.view];
 imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11));
 previousTouchedPoint = touchedPoint ;
}

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{

 CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
   float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}

【问题讨论】:

  • 请告诉我们它没有按预期工作。箭头是否指向正确的方向?它是否围绕错误的轴旋转?
  • 是的,它没有指向正确的方向。当我触摸 UIView 上的另一个点时,此箭头指向另一个方向。
  • 您的图像指向哪个方向?起来了吗?

标签: ios cocoa-touch uiimageview transform image-rotation


【解决方案1】:

我假设你想要一个箭头图像来指向你触摸的地方,我试过了,这就是我能想到的。我放置了一个带有向上箭头的图像视图(没有尝试从任何其他位置开始,日志给出正确的角度)并且在触摸不同位置时它会旋转并指向触摸位置。希望它有所帮助(尝试了一些旧数学:-))

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch=[[event allTouches]anyObject];
    touchedPoint= [touch locationInView:touch.view];
    CGFloat angle = [self getAngle:touchedPoint];
    imageView.transform = CGAffineTransformMakeRotation(angle);

}

-(CGFloat) getAngle: (CGPoint) touchedPoints
{
    CGFloat x1 = imageView.center.x;
    CGFloat y1 = imageView.center.y;

    CGFloat x2 = touchedPoints.x;
    CGFloat y2 = touchedPoints.y;

    CGFloat x3 = x1;
    CGFloat y3 = y2;

    CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
    CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));

    CGFloat angle = atanf(oppSide/adjSide);
    // Quadrant Identifiaction
    if(x2 < imageView.center.x)
    {
        angle = 0-angle;
    }


    if(y2 > imageView.center.y)
    {
        angle = M_PI/2 + (M_PI/2 -angle);
    }
     NSLog(@"Angle is %2f",angle*180/M_PI);
    return angle;

}

-anoop4real

【讨论】:

  • 这个答案完全适用于向上的箭头。但为什么它不适用于任何其他方向?
  • 嗨,这是我使用的想法,我在图像中心绘制了两条垂直和水平线。现在我编码使得通过图像中心绘制的垂直线以及图像视图框架将指向您点击的方向。尝试放任意一张图片,观察图片框,你可以看到它指向点击的方向,只是图片的位置让你感觉它没有指向正确的方向。希望你能理解,我可以贴一些我用过的笔记,现在没有。
【解决方案2】:

鉴于你告诉我的,我认为问题在于你没有在touchesBegan 中重置你的转换。试着把它改成这样,看看效果是否更好:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

 UITouch *touch=[[event allTouches]anyObject];
 touchedPoint= [touch locationInView:touch.view];
 imageViews.transform = CGAffineTransformIdentity;
 imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11));
 previousTouchedPoint = touchedPoint ;
}

【讨论】:

  • 问题是一样的,它旋转到任何其他方向。
【解决方案3】:

您需要这条线来“消除不连续性”吗?似乎 atan2f() 返回 +π 到 -π 之间的值。这些不会直接与CATransform3DMakeRotation()一起工作吗?

【讨论】:

  • 另外,-touchesBegan 只会被调用一次——您还需要覆盖 -touchesMoved 以持续更新。 (并确保您的所有 super 都来自您的覆盖。
【解决方案4】:

您需要的是箭头指向最后点击的点。为了简化和测试,我使用了轻击手势(但它类似于 touchBegan:withEvent:)。

在 viewDidLoad 方法中,我注册了手势:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; [self.view addGestureRecognizer:tapGesture]; [点击手势释放];

每次点击时调用的方法:

- (void)tapped:(UITapGestureRecognizer *)gesture { CGPoint imageCenter = mFlecheImageView.center; CGPoint tapPoint = [手势 locationInView:self.view]; 双 deltaY = tapPoint.y - imageCenter.y; 双 deltaX = tapPoint.x - imageCenter.x; 双角弧度 = atan2(deltaY, deltaX) + M_PI_2; mFlecheImageView.transform = CGAffineTransformMakeRotation(angleInRadians); }

一个键是+ M_PI_2,因为 UIKit 坐标的原点位于左上角(而在三角函数中,我们使用左下角)。

【讨论】:

  • 谢谢,但有同样的问题。该箭头指向任何随机方向。
  • 这是我为测试而创建的项目:dl.free.fr/e6PJtrhnr 也许我误解了你的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多