2013-01-05 111 views
2

我有一個UIScrollView具有縮放UIImageView,即它實現:在ScrollView中旋轉縮放ImageView?

- (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的縮放視圖中執行此操作嗎?

+0

使用KTOneFingerRotationGestureRecognizer旋轉 –

+0

@CoDEFRo如果你想旋轉滾動視圖,然後用添加手勢滾動視圖。 – Warewolf

回答

2

以下代碼正在工作。將手勢添加到scrollView而不是imageView。

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

這是一個很好的解決方法,但是我發現將旋轉與UIScrollView imageView合併是很困難的,因爲縮放轉換似乎會重置整個轉換。與答案無關,這是最好的方式來做到這一點,但我認爲如果你想旋轉,最好創建你自己的縮放imageView。 –

+0

你是救世主!我試圖找出幾個小時:) – abhinavroy23

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]; 
}