在ScrollView中旋转缩放ImageView?

时间:2013-01-05 01:40:31

标签: 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中的缩放视图执行此操作?

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:2)

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

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