Dua*_*der 3 iphone scale ios auto-rotation
我有一个自定义的UIViewController我在其上添加了一个UIImageView(在StoryBoard上),然后在UIScrollView中嵌入了UIImageView.我的程序将图像加载到imageView中并将其缩放到适当的位置.通过适当的配合,我的意思是:
我目前在viewWillAppear中这样做.下面是我的方法代码和计算正确zoomScale的辅助方法:
-(float) findZoomScale {
float widthRatio = self.view.bounds.size.width / self.imageView.image.size.width;
float heightRatio = self.view.bounds.size.height / self.imageView.image.size.height;
float ratio;
if (widthRatio > heightRatio) ratio = widthRatio;
else ratio = heightRatio;
return ratio;
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
self.imageView.image = self.image;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
self.scrollView.contentSize = self.imageView.image.size;
self.scrollView.zoomScale = [self findZoomScale];
[self.scrollView flashScrollIndicators];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.photoURL]];
self.imageView.image = self.image;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width,
self.imageView.image.size.height);
self.scrollView.delegate = self;
self.scrollView.zoomScale = 1.0;
self.navigationItem.title = self.photoTitle;
self.scrollView.autoresizesSubviews = YES;
self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
Run Code Online (Sandbox Code Playgroud)
无论iPhone是横向还是纵向,此代码都可用于初始加载图像.但是当我用已经加载的图像旋转iPhone时,它没有正确地重新缩放.我试图在didRotateFromInterfaceOrientation中进行重新缩放,使用scrollView的zoomScale属性进行重新缩放,但我无法使其工作.这是重新调整的正确位置吗?如果是这样,我该怎么做?谢谢.
杜安
Pau*_*aul 18
我只是遇到了这个问题(我假设你也在使用CS193p 4),并zoomScale在调整UIScrollView contentSize旋转之前将其设置为1 来解决它.
例如:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//reset zoomScale back to 1 so that contentSize can be modified correctly
self.scrollView.zoomScale = 1;
// reset scrolling area equal to size of image
self.scrollView.contentSize = self.imageView.image.size;
//reset the image frame to the size of the image
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
//set the zoomScale to what we actually want
self.scrollView.zoomScale = [self findZoomScale];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5532 次 |
| 最近记录: |