【问题标题】:Rotate image on center using one finger touch使用一根手指触摸在中心旋转图像
【发布时间】:2011-03-29 06:08:20
【问题描述】:

我想用一根手指触摸中心点旋转下面的图像...

当我使用触摸旋转图像时,我想用标签显示图像的值。

我已经完成了图像旋转,但问题是如何根据旋转设置图像的值。

旋转的角度增加了,所以我不能设置这个值。

谁能帮帮我?

代码如下

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;

谁能帮帮我?

【问题讨论】:

  • “图像的价值”是什么意思?
  • 我的图像中显示了从 0 到 50 的值。所以当我旋转图像时,特定值应该显示在标签中。

标签: iphone image rotation


【解决方案1】:

不完全确定你在追求什么;但试试这段代码。

如果您创建一个名为“Rotation”的新的基于视图的应用程序项目,并将 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 您需要查看 CGAffineTransformMakeTranslation 并将其结果与上述代码中的 cgaRotate 结合起来。你必须有一些方法来切换触摸拖动是否应该影响旋转或平移。
  • @Wex 这段代码对我很有帮助,但我有一个问题。我已经在我的应用程序中实现了这段代码,它运行良好。在这段代码中,如果我将手指放在屏幕旋转开始的任何位置,但我想要这样的功能,其中只有当我将手指放在“m_block”视图上时才会发生旋转。那怎么可能呢?
  • @iPhone 我现在无法测试它;但请尝试:在获得“位置”的三个地方之后放置代码: if([self.view hitTest:location withEvent:_event] != m_block) return;
  • 您可以使用 M_PI 代替 3.14,但您的肉眼不会真正注意到差异。感谢您的代码。
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
相关资源
最近更新 更多