So I get that changing the bounds on the UIScrollView pans, but what is actually happening when it zooms? Does it set it's subviews transforms? The reason I ask is that I am trying to change the contentSize while the view is zoomed and I am getting some strange behavior.
I have setup a subclass of UIScrollView with a contentView variable that is simply a UIView added as a subview of the UIScrollView. I then add other UIViews to the contentView. I check to see if the added view extends beyond the frame of the contentView. If it does, I adjust the contentView's frame accordingly and set the scrollView's contentSize to the contentView's frame size. I then zoom to the contentView's frame if needed.
What I expect to happen here is that as I add views to the contentView the contentView's frame and scrollView's contentSize should grow to accomodate any view I insert, while zooming out to show the full contentView.
If I don't zoom while adding the subviews I get the appropriate frame/contentSize. However, if I zoom while adding the subviews, the frame/contentSize appear to be correct and the view zooms as I would expect it to, however the scrollView region seems to get way too large, meaning I can scroll well past the contentView, which I don't quite understand seeing as the scrollView's contentSize ends up the same either way. So I must not understand what is actually going on when I zoom. Should I adjust the contentSize I am setting by the zoomScale? When I try that it initially looks ok, but then after I zoom manually the large scrollView region returns.
Any help would be greatly appreciated, this one has me stumped!
I have setup a subclass of UIScrollView with a contentView variable that is simply a UIView added as a subview of the UIScrollView. I then add other UIViews to the contentView. I check to see if the added view extends beyond the frame of the contentView. If it does, I adjust the contentView's frame accordingly and set the scrollView's contentSize to the contentView's frame size. I then zoom to the contentView's frame if needed.
Code:
- (void)packSubview:(UIView *)view {
// Animate things slowly so that I can see what is happening
[UIView beginAnimations:@"packView" context:nil];
[UIView setAnimationDuration:1.5];
// View's frame is initially set assuming an origin at the center of the view
view.frame = CGRectMake(self.centerOfContentView.x+view.frame.origin.x,
self.centerOfContentView.y+view.frame.origin.y,
view.frame.size.width,
view.frame.size.height);
// Check if view's origin is negative in either direction, if so, store the offset
CGPoint offset = CGPointZero;
BOOL doMove = NO;
if(view.frame.origin.x < 0) {
offset.x = -view.frame.origin.x;
}
if(view.frame.origin.y < 0) {
offset.y = -view.frame.origin.y;
}
[self.scrollView.contentView addSubview:view];
// Move subviews of contentView, such that the new subview doesn't have a negative origin in either direction, moving them by the offset
// At the same time check to see if the size of the contentView needs to be extended to accomodate the shift
CGSize size = self.scrollView.contentView.frame.size;
for(UIView *subview in self.scrollView.contentView.subviews) {
subview.center = CGPointMake(subview.center.x+offset.x,
subview.center.y+offset.y);
// Record the size that would be required such that the view does not extend beyond the frame of the contentView
CGSize sizeNeededForView = CGSizeMake(subview.frame.origin.x+subview.frame.size.width,
subview.frame.origin.y+subview.frame.size.height);
// If the size required for the view is greater than the current size save that size
if(sizeNeededForView.width > size.width)
size.width = sizeNeededForView.width;
if(sizeNeededForView.height > size.height)
size.height = sizeNeededForView.height;
}
// Move the center position of the view for the next view that is added
centerOfContentView_.x += offset.x;
centerOfContentView_.y += offset.y;
// Set the frame of the contentView with the size stored earlier
self.scrollView.contentView.frame = CGRectMake(0, 0, size.width, size.height);
// Set the contentSize of the scrollView to be the size of the contentView
self.scrollView.contentSize = self.scrollView.contentView.frame.size;
// Find the zoomScale needed to show the entire contentView
CGFloat scaleX = self.frame.size.width/self.scrollView.contentView.frame.size.width;
CGFloat scaleY = self.frame.size.height/self.scrollView.contentView.frame.size.height;
CGFloat scale = scaleX;
if(scaleY < scale)
scale = scaleY;
// If the scale is less than the current minimumZoomScale set the minimumZoomScale and zoom to the contentView's frame
if(scale < self.scrollView.minimumZoomScale) {
BOOL doZoom = self.scrollView.zoomScale == self.scrollView.minimumZoomScale;
self.scrollView.minimumZoomScale = scale;
// Only zoom if the scrollView is already zoomed all the way out. It would be disruptive if the user zoomed in and we changed that on them.
if(doZoom)
[self.scrollView zoomToRect:self.scrollView.contentView.frame animated:YES];
}
[UIView commitAnimations];
}
What I expect to happen here is that as I add views to the contentView the contentView's frame and scrollView's contentSize should grow to accomodate any view I insert, while zooming out to show the full contentView.
If I don't zoom while adding the subviews I get the appropriate frame/contentSize. However, if I zoom while adding the subviews, the frame/contentSize appear to be correct and the view zooms as I would expect it to, however the scrollView region seems to get way too large, meaning I can scroll well past the contentView, which I don't quite understand seeing as the scrollView's contentSize ends up the same either way. So I must not understand what is actually going on when I zoom. Should I adjust the contentSize I am setting by the zoomScale? When I try that it initially looks ok, but then after I zoom manually the large scrollView region returns.
Any help would be greatly appreciated, this one has me stumped!