0

我有一个 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;
}

在此处输入图像描述

4

4 回答 4

5

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

-(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

于 2012-07-20T10:44:41.617 回答
1

鉴于您告诉我的内容,我认为问题在于您没有在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 ;
}
于 2012-07-12T13:12:38.157 回答
1

你需要这条线来“消除不连续性”吗?似乎atan2f()返回 +π 到 -π 之间的值。那些不会直接使用CATransform3DMakeRotation()吗?

于 2012-07-17T07:33:21.050 回答
1

您需要的是箭头指向最后一个点击点。为了简化和测试,我使用了轻击手势(但它类似于 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 坐标的原点位于左上角(而在三角函数中,我们使用左下角)。

于 2012-07-17T18:38:05.973 回答