【问题标题】:Rotate Zooming ImageView in ScrollView?在 ScrollView 中旋转缩放 ImageView?
【发布时间】:2012-12-19 12:16:42
【问题描述】:

我有一个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 中使用缩放视图执行此操作?

【问题讨论】:

  • 使用 KTOneFingerRotationGestureRecognizer 进行旋转
  • @CoDEFRo 如果你想旋转scrollView然后使用添加手势到scrollView。

标签: iphone ios cocoa-touch uiscrollview uigesturerecognizer


【解决方案1】:

以下代码正在运行。将手势添加到 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)

【讨论】:

  • 这是一个很好的解决方法,但是我发现很难将旋转与 UIScrollView imageView 结合起来,因为缩放转换似乎重置了整个转换。与答案无关,这是最好的方法,但我认为如果你想进行旋转,最好创建自己的缩放 imageView。
  • 你是救世主!我试图弄清楚这个表格小时:)
【解决方案2】:
-(void)rotate:(UIRotationGestureRecognizer *)sender
{   
    [self bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]]; 
    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 
    {       
        lastRotation = 0.0;
        return;
    }
    CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);  
    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);   
    [[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];    
    lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
    lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多