用一只手指触摸屏幕以中心为轴旋转图像

11
我想用一根手指触摸来围绕中心点旋转下面的图像...
并且我想在旋转图像时用标签显示图像的值。
我已经完成了图像旋转,但问题是如何根据旋转设置图像的值。
旋转角度不断增加,因此我无法设置该值。
有人能帮忙吗?
以下是代码:
float fromAngle = atan2(firstLoc.y-imageView.center.y, 
                            firstLoc.x-imageView.center.x);

    NSLog(@"From angle:%f",fromAngle);
    float toAngle = atan2( currentLoc.y-imageView.center.y, 
                          currentLoc.x-imageView.center.x);

    NSLog(@"to Angle:%f",toAngle);
    // So the angle to rotate to is relative to our current angle and the
    // angle through which our finger moved (to-from)
    float newAngle =angle+(toAngle-fromAngle);


    NSLog(@"new angle:%.2f",newAngle);


 CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);

 imageView.transform=cgaRotate;


angle = newAngle;

有人能帮我吗?


“value of the image” 是什么意思? - Wex
我有一张图片,其中显示了从0到50的值。因此,当我旋转图像时,标签中应该显示特定的值。 - darshan
1个回答

22

我不是完全确定你所需要的内容;但是可以试试这段代码。

如果你创建一个名为“Rotation”的基于View的应用程序项目,并替换RotationViewController.h和.m中的代码,使用以下代码,你将获得一个绿色方块,可以使用你的计算进行旋转。你可以将绿色方块UIView替换为UIImageView,或者任何其他想要旋转的东西。


RotationViewController.h

#import <UIKit/UIKit.h>

@interface RotationViewController : UIViewController
{
    UIView* m_block;
    UILabel* m_label;
    CGPoint m_locationBegan;
    float m_currentAngle;
}

- (float) updateRotation:(CGPoint)_location;

@end

RotationViewController.m

#import "RotationViewController.h"

double wrapd(double _val, double _min, double _max)
{
    if(_val < _min) return _max - (_min - _val);
    if(_val > _max) return _min - (_max - _val);
    return _val;
}

@implementation RotationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect blockFrame = CGRectMake(0, 0, 200, 200);
    m_block = [[UIView alloc] initWithFrame:blockFrame];
    m_block.backgroundColor = [UIColor greenColor];
    m_block.center = self.view.center;
    [self.view addSubview:m_block];
    [m_block release];
    
    CGRect labelFrame = CGRectMake(0, 0, 320, 30);
    m_label = [[UILabel alloc] initWithFrame:labelFrame];
    m_label.text = @"Loaded";
    [self.view addSubview:m_label];
}

- (void) touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_locationBegan = location;
}

- (void) touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    [self updateRotation:location];
}

- (void) touchesEnded:(NSSet *)_touches withEvent:(UIEvent *)_event
{
    UITouch* touch = [_touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    m_currentAngle = [self updateRotation:location];
}

- (float) updateRotation:(CGPoint)_location
{
    float fromAngle = atan2(m_locationBegan.y-m_block.center.y, m_locationBegan.x-m_block.center.x);
    float toAngle = atan2(_location.y-m_block.center.y, _location.x-m_block.center.x);
    float newAngle = wrapd(m_currentAngle + (toAngle - fromAngle), 0, 2*3.14);

    CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
    m_block.transform = cgaRotate;
    
    int oneInFifty = (newAngle*50)/(2*3.14);
    
    m_label.text = [NSString stringWithFormat:@"Angle: %f 1in50: %i", newAngle, oneInFifty];

    return newAngle;
}

@end


@wex,现在如果我想将这个图像移动到屏幕的其他位置,我该怎么做? - raaz
1
@raaz,你需要研究一下CGAffineTransformMakeTranslation,并将其结果与上面代码中的cgaRotate相结合。但是,你必须有一种方法来切换触摸拖动是否应该影响旋转或平移。 - Wex
@Wex 这段代码对我非常有帮助,但我有一个问题。我已经在我的应用程序中实现了这段代码,它运行良好。在这段代码中,如果我在屏幕上的任何地方放手指,旋转就会开始,但我想要这样一种功能,只有当我把手指放在“m_block”视图上时才会发生旋转。那么这怎么可能呢? - iPhone
1
@iPhone 我现在没有测试的条件,但是请尝试:在获取“location”的三个位置后加入以下代码:if([self.view hitTest:location withEvent:_event] != m_block) return; - Wex
你可以使用M_PI代替3.14,但是肉眼几乎看不出区别。感谢提供代码。 - Underdog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接