好吧,这似乎很容易,但是我已经花了几个小时试图做到这一点。
我正在尝试开发一个带有图像的圆形控件并旋转该控件,就像大型音响设备的音量控件一样。然后要旋转图像,我使用UIPanGestureRecognizer
和ATAN2
。
使用图像的中心(如0,0坐标)以及图像的原点和大小来测量我的动作区域。之后,计算图像中心与X和Y的夹角(我的0,0)。如果“触摸”不在图像的边界内,则什么也不做,但是如果在图像的内部,我要旋转它,但是仍然无法正常工作。
编辑:我不想在此过程中使用两只手指,我只需要使用一根手指。
UIRotateGestureRecognizer使用两个,在这里可以采用这种方式。
这是我的代码:
@property (strong, nonatomic) IBOutlet UIImageView *myMainIndicatorRoulett;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanWithGestureRecognizer:)];
[self.myTestView addGestureRecognizer:panGestureRecognizer];
_myMainIndicatorRoulett.image = [TheColoristImages imageOfColorIndicatorWithIndicatorAngle:(CGFloat)90.0];
}
-(void)handlePanWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer
{
double a1;
double a2;
a1 = [panGestureRecognizer locationInView:self.view].x;
a2 = [panGestureRecognizer locationInView:self.view].y;
double b1;
double b2;
b1 = _myMainIndicatorRoulett.frame.origin.x;
b2 = _myMainIndicatorRoulett.frame.origin.y;
double c1;
double c2;
c1 = b1 + _myMainIndicatorRoulett.frame.size.width;
c2 = b2 + _myMainIndicatorRoulett.frame.size.height;
if ((a1 > b1) && (a1 < c1) && (a2 > b2) && (a2 < c2))
{
double Cx;
double Cy;
Cx = _myMainIndicatorRoulett.center.x;
Cy = _myMainIndicatorRoulett.center.y;
double Tx;
double Ty;
Tx = [panGestureRecognizer locationInView:self.view].x - Cx;
Ty = [panGestureRecognizer locationInView:self.view].y - Cy;
double myAngle = atan2(Ty, Tx);
_myMainIndicatorRoulett.transform = CGAffineTransformRotate(_myMainIndicatorRoulett.transform, myAngle * 3.14 / 180);
}
}
最佳答案
您的问题在最后一行:
_myMainIndicatorRoulett.transform = CGAffineTransformRotate(_myMainIndicatorRoulett.transform, myAngle * 3.14 / 180);
当您使用CGAffineTransformRotate(s,a)时,它会添加该角度,而不设置它。这就是为什么它会变得疯狂。每次调用该函数时,它都会在旋转中添加“ myAngle”度,使其看起来像在旋转。您要设置角度。
您要使用此:
_myMainIndicatorRoulett.transform = CGAffineTransformMakeRotation(myAngle);
关于ios - 如何使用UIPanGestureRecognizer旋转图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32175588/