开发者

iOS zoomToRect with duration [duplicate]

开发者 https://www.devze.com 2023-04-12 05:09 出处:网络
This question already has answers here: Closed 10 years ago. Possible Duplicate: Any way of changing the duration of zoomToRect for UIScrollView?
This question already has answers here: Closed 10 years ago.

Possible Duplicate:

Any way of changing the duration of zoomToRect for UIScrollView?

Is there any way to replicate the behaviour of [UIScrollView zoomToRect:zoomRect animated:YES] suc开发者_高级运维h that the animation lasts for a given duration?


You can put this in your UIScrollView subclass:

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


How about using:

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

The downside is that you need to calculate the exact CGPoint to achieve the correct offset you want to get to.


I managed to implement zoomToRect using some CA3DTransforms. I will paste the code here in case anyone is interested.

I had to keep the a reference to the original frame of the scrollView for this to work.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号