iphone - 在ScrollView中旋转缩放ImageView?

标签 iphone ios cocoa-touch uiscrollview uigesturerecognizer

我有一个 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/

相关文章:

iphone - 联网的 iOS 应用程序

ios - iOS 7.1 与 iOS 7 上 uiwebview 的内存使用情况

iphone - 以编程方式在自定义 UIView 子类中创建 UIButton,如何访问文件所有者以添加目标方法?委托(delegate)们?

iphone - UITableViewCell 可重用性导致问题 [带有图像]!

iphone - NSString之间可以插入图片吗?

iphone - AVCapture追加SampleBuffer

iphone - 有没有用于翻译 Localized.strings 文件的应用程序?

ios - iPhone 上的 HTTP 实时流媒体服务器

ios - 如何在头文件中声明公共(public) NS_Enum 属性

cocoa-touch - 动画UISegmentedControl的框架