我有一个 UIScrollView
,它有一个缩放 UIImageView
,即它实现:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
我正在尝试将 UIRotationGestureRecognizer
添加到此 imageView
,具体操作如下:
_rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self.imageView addGestureRecognizer:_rotationGestureRecognizer];
-(void)rotate:(id)sender
{
UIRotationGestureRecognizer *rotationGestureRecognizer = [(UIRotationGestureRecognizer*)sender retain];
if(rotationGestureRecognizer.state == UIGestureRecognizerStateEnded)
{
self.lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (self.lastRotation - rotationGestureRecognizer.rotation);
rotationGestureRecognizer.view.transform = CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation);
self.lastRotation = rotationGestureRecognizer.rotation;
[rotationGestureRecognizer release];
}
我只是想知道,是否可以做到这一点?似乎 UIScrollView
阻止了对我的 UIImageView
的触摸,因为什么也没有发生。 Apple 是否建议不要在 UIScrollView
中使用缩放 View 来执行此操作?
最佳答案
以下代码有效。将手势添加到scrollView而不是imageView。
UIRotationGestureRecognizer* _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self.scrollView addGestureRecognizer:_rotationGestureRecognizer];
Swift 5 版本:
let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.rotate(_:)))
scrollView.addGestureRecognizer(rotationGestureRecognizer)
关于iphone - 在ScrollView中旋转缩放ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167646/