iOS zoomToRect持续时间

时间:2011-10-10 17:16:20

标签: ios scrollview

  

可能重复:
  Any way of changing the duration of zoomToRect for UIScrollView?

有没有办法复制[UIScrollView zoomToRect:zoomRect animated:YES]的行为,以便动画持续给定的持续时间?

3 个答案:

答案 0 :(得分:18)

您可以将其放在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];
}

答案 1 :(得分: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以达到您想要达到的正确偏移量。

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