- [UIScrollView zoomToRect: animated:] weird behavior when contentSize <bounds.size
Can anyone accurately describe the behavior -[UIScrollView zoomToRect:animated:]? This method really seems complicated, but Apple's documentation about it is very scarce.
I get unpredictable behavior of this method when the size of the content is less than the scroll size in width and / or height. In some cases, this method causes the scroll view to have a negative content offset, when it should be 0. After going through slightly different rectangles, it leaves the content offset at 0, as I expected.
To demonstrate this strange behavior, I created a sample project with a scrolling view of the size (200, 200) containing the content view of the size (100, 100) . I would expect that scaling for the direct ((0, 0), (200, 200)) content content should leave the content in the upper left corner (i.e. nothing should happen ). However, in reality this leads to the fact that the content scrolls to the right to the right of the borders of the scroll view (content offset (-100, -100)). Why is this happening?

Here is the code in my sample project:
@implementation RootViewController
- (void)loadView {
self.view = [[UIView alloc] init];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
self.scrollView.delegate = self;
self.scrollView.backgroundColor = [UIColor whiteColor];
self.scrollView.minimumZoomScale = .5;
self.scrollView.maximumZoomScale = 4;
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.contentView.backgroundColor = [UIColor redColor];
[self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)]];
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.frame.size;
[self.view addSubview:self.scrollView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.contentView;
}
- (void)handleTap {
[self.scrollView zoomToRect:CGRectMake(0, 0, 200, 200) animated:YES]; // Try changing to 199, 199 to get completely different behavior
}
@end
! , UIScrollView , . , , , , , , .
- , , . , , . " - -[UIScrollView zoomToRect:animated:]?"? .
, ( ):
@implementation TBScrollView // Subclass of UIScrollView
- (void)setContentOffset:(CGPoint)contentOffset {
contentOffset.x = MAX(contentOffset.x, 0);
contentOffset.y = MAX(contentOffset.y, 0);
[super setContentOffset:contentOffset];
}
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
contentOffset.x = MAX(contentOffset.x, 0);
contentOffset.y = MAX(contentOffset.y, 0);
[super setContentOffset:contentOffset animated:animated];
}
@end
: , , , ( ). .
2: , , -zoomToRect:animated:. BOOL, YES -zoomToRect:animated: NO, animated NO. NO -scrollViewDidEndScrollingAnimation: ( if -zoomToRect:animated: ) -scrollViewDidEndZooming:withView:atScale: ( if -zoomToRect:animated: ). , , iOS, , , :)