小编Dua*_*der的帖子

在自动旋转时重置UIScrollView的zoomScale属性

我有一个自定义的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 …
Run Code Online (Sandbox Code Playgroud)

iphone scale ios auto-rotation

3
推荐指数
1
解决办法
5532
查看次数

标签 统计

auto-rotation ×1

ios ×1

iphone ×1

scale ×1