views:

518

answers:

2

I have an instance of UIScrollview containing an instance of UIView. The UIView is just a container for a horizonal array of UIImageView instances. - Zooming is provided by UIScrollView and UIScrollViewDelegate. I would like to constrain zooming to occur only along the horizontal axis with no vertical scalling at all. How to I do this?

Is there a way, for example, to subclass UIView and override the appropriate method to prevent vertical scaling? I like this approach but I am unclear on which method to override and what that overridden method should actually do.

Cheers, Doug

+1  A: 

Using OS 3.0, you can tell the zoom to zoom to a rect in the scrollview. I have this in my logic that detects taps.

CGRect zoomRect = [self zoomRectForScale:newScale withCenter:CGPointMake(tapPoint.x, tapPoint.y) inScrollView:scrollView];
[scrollView zoomToRect:zoomRect animated:YES];

The for the other part, you will have to stretch your image views by the ratio that the new frame has against the original, and center it in the same center point. You can do this in an animation timed the same as the zoom animation so that it looks right, but I think this will be the only way to do it.

coneybeare
Thanks for this coneybeare. So that I'm sure I understand this method's usage, if I were to use the scrollview.bounds as the rect I would assume that would pin the dimensions of the view being scrolled to the dimensions of the scrollview, correct?Cheers,Doug
dugla
coneybeare,Actually, from the looks of your code snippet it appears you are handling all zooming since you are calling zoomToRect:animated yourself, rather than simply overriding it. I'd prefer to let UIScrollView handle all that and just insert my constraint. Will you approach require me to implement my own pan/zoom functionality. Could you provide a bit more enlightenment.Cheers,Doug
dugla
+1  A: 

Similar to what I describe in this answer, you can create a UIView subclass and override the -setTransform: accessor method to adjust the transform that the UIScrollView will try to apply to your UIView. Set this UIView to host your content subviews and make it the subview of the UIScrollView.

Within your overridden -setTransform:, you'll need to take in the transform that the UIScrollView would like to apply and adjust it so that the scaling only takes effect in one direction. From the documentation on how CGAffineTransform matrices are constructed, I believe the following implementation should constrain your scaling to be just along the horizontal direction:

- (void)setTransform:(CGAffineTransform)newValue;
{
 CGAffineTransform constrainedTransform = CGAffineTransformIdentity;
 constrainedTransform.a = newValue.a;
 [super setTransform:constrainedTransform];
}
Brad Larson
Lovely. This is EXACTLY what I've been hunting for. Thanks a ton. Cheers.
dugla
Works like a charm Brad! Coolness. Thanks again.
dugla