gpt4 book ai didi

iphone - 使用一根手指触摸在中心旋转图像

转载 作者:行者123 更新时间:2023-12-03 18:28:21 24 4
gpt4 key购买 nike

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

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

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

旋转角度增加,因此无法设置该值。

谁能帮帮我吗?

代码如下

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;

谁能帮帮我吗?

最佳答案

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

如果您创建一个名为“Rotation”的新的基于 View 的应用程序项目,并替换 RotationViewController.h 和 .m 中的代码,您将获得一个绿色 block ,您可以使用计算来旋转该 block 。您可以将绿色 block 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
<小时/>

关于iphone - 使用一根手指触摸在中心旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5468559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com