UIImageView在触摸方向上旋转动画

时间:2012-07-11 11:16:20

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

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

enter image description here

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 ;
}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

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

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];

每个点击调用的方法:

- (void)tapped:(UITapGestureRecognizer *)gesture
{
    CGPoint imageCenter = mFlecheImageView.center;
    CGPoint tapPoint = [gesture locationInView:self.view];

    double deltaY = tapPoint.y - imageCenter.y;
    double deltaX = tapPoint.x - imageCenter.x;
    double angleInRadians = atan2(deltaY, deltaX) + M_PI_2;

    mFlecheImageView.transform = CGAffineTransformMakeRotation(angleInRadians);
}

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

相关问题