2011-10-10 53 views

回答

17

你可以把這個在您的UIScrollView子類:

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated 
{ 
    [UIView animateWithDuration:(animated?0.3f:0.0f) 
          delay:0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         [super zoomToRect:rect animated:NO]; 
        } 
        completion:nil]; 
} 
+2

這太棒了。 – silviupop

0

如何使用:

[UIView beginAnimations:@"" context:nil]; 
[UIView setAnimationDuration:2.0]; //Or any other duration 

[theScroll setContentOffset:offsetPoint]; //offsetPoint is a CGPoint that defines the point you want your scroller to show 

[UIView commitAnimations]; 

的缺點是,你需要計算精確CGPoint達到你想要得到正確的偏移量。

+0

這個我試過的形式給出,還必須設置縮放級別和生成的動畫不等同於zoomToRect。 – silviupop

0

我設法使用一些CA3DTransforms來實現zoomToRect。如果有人感興趣,我會在這裏粘貼代碼。

我不得不保留一個引用scrollView的原始框架,以使其工作。

rect.origin.x = ((int)rect.origin.x) % (int)self.initialFrame.size.width;  
float scale = MIN(self.initialFrame.size.width/rect.size.width,self.initialFrame.size.height/rect.size.height); 

CGSize scaledFrameSize = CGSizeMake(self.initialFrame.size.width/scale, self.initialFrame.size.height/scale); 

CGPoint middleOfFrame = CGPointMake(self.initialFrame.size.width/2 ,self.initialFrame.size.height/2); 
CGPoint transformPoint = CGPointMake(rect.origin.x + scaledFrameSize.width/2,rect.origin.y + scaledFrameSize.height/2); 
CGPoint offsetToCenter = CGPointMake((scaledFrameSize.width - rect.size.width)/2 * scale,(scaledFrameSize.height - rect.size.height)/ 2 * scale); 

[UIView animateWithDuration:1 animations:^ { 
    self.layer.transform = CATransform3DConcat(CATransform3DConcat(CATransform3DConcat(CATransform3DMakeTranslation(middleOfFrame.x,middleOfFrame.y, 0), 
               CATransform3DMakeTranslation(-transformPoint.x, -transformPoint.y,0)), 
               CATransform3DMakeScale(scale, scale, 1)), 
               CATransform3DMakeTranslation(offsetToCenter.x, offsetToCenter.y, 0)); 
}];