想要实现一个让一张图片围绕中心点旋转的功能。自己用UIPanGestureRecognizer手势实现了一个,通过计算手势移动的位置,计算出手指距图片中心点的偏移度,然后让UIImageView偏移相同的度数即可。测试了一下,发现虽然图片旋转了,但是中心点看起来好像一直在变化,将中心点的坐标打印出来却没有变化,可能是图片是方形的原因,下次用圆形图片尝试一下。
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)]; [_imageView addGestureRecognizer:pan]; - (void)panView:(UITapGestureRecognizer*)pan { CGAffineTransform startTransform; CGPoint point = [pan locationInView:self.view]; NSLog(@"point = %@", NSStringFromCGPoint(point)); //计算偏转角度 float deflectionAngle = atan2(point.y-_imageView.frame.origin.y, point.x-_imageView.frame.origin.x); switch (pan.state) { case UIGestureRecognizerStateBegan: startTransform = _imageView.transform; break; case UIGestureRecognizerStateChanged: _imageView.transform = CGAffineTransformRotate(startTransform, deflectionAngle); break; case UIGestureRecognizerStateEnded: break; default: break; } }
时间: 2024-10-19 01:01:57