2

我有UIScrollView一个 zooming UIImageView,即它实现:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

我正在尝试添加一个UIRotationGestureRecognizerimageView我这样做如下:

_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 是否建议不要在 a 中使用缩放视图来执行此操作UIScrollView

4

2 回答 2

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];
}
于 2013-01-05T05:01:49.960 回答
2

以下代码正在运行。将手势添加到 scrollView 而不是 imageView。

 UIRotationGestureRecognizer* _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
 [self.scrollView addGestureRecognizer:_rotationGestureRecognizer];

斯威夫特 5 版本:

let rotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(self.rotate(_:)))
scrollView.addGestureRecognizer(rotationGestureRecognizer)
于 2013-01-05T05:09:16.463 回答