Problems with scrolling and zooming after rotating a UIImageView
I have the same problem as in HERE .. Please Help!
I have a UIViewController that supports scrolling and zooming of a UIImageView. It works great.
I’m trying to add support for changes in orientation. For various reasons, I need to do this by rotating just the UIImageView and not the UIViewController.
I have the image rotation working, but if the user pinch-zooms when the image is rotated, the image’s size and location go bonkers (e.g., the images jumps to the side, gets too small, doesn’t seem to know where the extents of the scroll view are, etc).
The core contents of the h file:
@interface ImageViewController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *imageView;
}
@property (nonatomic, assign) UIScrollView *scrollView;
@property (nonatomic, assign) UIImageView *imageView;
@end
Support for zooming:
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
Support for scrolling (set after the image is loaded):
[scrollView setContentSize: CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height)];
Support for rotating the image when the orientation changes:
-(void)orientationChanged:(NSNotification *)note
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
CGFloat angle = 0;
switch (orientation)
{
case UIDeviceOrientationLandscapeLeft:
angle = M_PI/2.0f;
break;
case UIDeviceOrientationLandscapeRight:
angle = -M_PI/2.0f;
break;
case UIDeviceOrientationPortraitUpsideDown:
angle = M_PI;
break;
default:
break;
}
self.imageView.transform = CGAffineTransformMakeRotation(angle);
}
I suspect the problem is that the scrollView is confused because the size of the image has changed due to the rotation, but I’m clueless as to how to fix it. Any ideas? FWIW, the zooming d开发者_StackOverflowoes work if I change the orientation, but then change it back to "vertical" before zooming.
Redraw your image view by removing it from the srollView and read it again.
精彩评论