Ios 如何使用UIPangestureRecognitor旋转图像
嗯,这似乎很容易,但我已经花了好几个小时试着这么做了。 我试图开发一个圆形控件,带有图像并旋转控件,就像大型音响设备的音量控制一样。然后,为了旋转图像,我使用Ios 如何使用UIPangestureRecognitor旋转图像,ios,objective-c,xcode,Ios,Objective C,Xcode,嗯,这似乎很容易,但我已经花了好几个小时试着这么做了。 我试图开发一个圆形控件,带有图像并旋转控件,就像大型音响设备的音量控制一样。然后,为了旋转图像,我使用uipangestrerecognizer和ATAN2 使用图像的中心(如0,0坐标)以及图像的原点和大小来测量我的动作区域。计算X和Y与图像中心的角度(my 0,0)后。如果触摸超出图像的边界,什么也不做,但是如果在图像内部,我想旋转它,但是,仍然无法工作。 编辑:我不想在这个过程中使用两个手指,我只需要使用一个手指 UIRotateSt
uipangestrerecognizer
和ATAN2
使用图像的中心(如0,0坐标)以及图像的原点和大小来测量我的动作区域。计算X和Y与图像中心的角度(my 0,0)后。如果触摸超出图像的边界,什么也不做,但是如果在图像内部,我想旋转它,但是,仍然无法工作。
编辑:我不想在这个过程中使用两个手指,我只需要使用一个手指
UIRotateSteureRecogener使用两个,在这里可以采用这种方式
这是我的密码:
@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);
}
}
@property(强,非原子)IBUIImageView*myMainIndicatorRoulett;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
UIPangestureRecognitzer*PangestureRecognitzer=[[UIPangestureRecognitzer alloc]initWithTarget:self action:@selector(HandlePanWithGestureRecognitzer:)];
[self.myTestView addgestureerecognizer:pangestureerecognizer];
_MyMain IndicatorRoulett.image=[颜色指示器的颜色估计图像,带指示器范围:(CGFloat)90.0];
}
-(无效)带有手势识别器的手持面板:(UIPangestureRecognitor*)PangestureRecognitor
{
双a1;
双a2;
a1=[PangestureRecognitor locationInView:self.view].x;
a2=[PangestureRecognitor locationInView:self.view].y;
双b1;
双b2;
b1=_mymain指示器roulett.frame.origin.x;
b2=_mymain指示器roulett.frame.origin.y;
双c1;
双c2;
c1=b1+_mymain指示器箭头:框、尺寸、宽度;
c2=b2+_mymain指示器箭头:框、尺寸、高度;
如果((a1>b1)和&(a1b2)和&(a2
您的问题在最后一行:
_myMainIndicatorRoulett.transform = CGAffineTransformRotate(_myMainIndicatorRoulett.transform, myAngle * 3.14 / 180);
使用CGAffineTransformRotate(s,a)时,它会添加该角度,而不会设置该角度。这就是为什么它会变得疯狂。每次调用该函数时,它都会将“myAngle”度数添加到旋转中,使旋转看起来像是在旋转。您需要设置角度
您要使用以下命令:
_myMainIndicatorRoulett.transform = CGAffineTransformMakeRotation(myAngle);
UIView上的一个手指旋转:
检查此repo,它是一个定制的手势识别器,可为您处理单手指旋转。为什么不使用
ui旋转手势识别器
?这将使事情变得简单。我想做的是用一个手指旋转程序,而不是用两只手来使用iPhone。另一方面,用两个手指是最简单的方法,但在这里,只用一个手指更好,这就是为什么我如此挣扎的原因。你对此有什么想法吗???解释一下你的代码错在哪里。当然@Wain,我想用手指旋转图像,就像在屏幕上滑动手指一样旋转图像,但是当我尝试时,图像旋转得非常快,或者缓慢,或者只是半旋转,就这样。。。或者首先,记录Tx和Ty值,以及根据它们计算的角度。然后你可以看看你是否有一个正常化问题或规模问题(或两者都有)。我不明白区别,现在就像一个魅力!