【问题标题】:How do I Change minimum Zoom scale on orientation change?如何在方向更改时更改最小缩放比例?
【发布时间】:2011-01-13 20:36:09
【问题描述】:

我正在尝试设置一个与 iPhone 上的照片应用程序非常相似的应用程序。我遇到的问题是,如果当前缩放比例小于最小值,我无法找到设置最小缩放比例并强制当前缩放比例回到最小值的好方法。

这是我当前设置滚动视图的方式...

- (void)viewDidLoad{

NSData * imageData = [[[NSData alloc] autorelease] initWithContentsOfURL: [NSURL URLWithString: imageURL]];
UIImage *babe = [UIImage imageWithData:imageData];
babeView = [[UIImageView alloc]
            initWithImage:babe];
[self.view addSubview:babeView];
UIBabeScrollView* myScrollview = (UIBabeScrollView*)self.view;
myScrollview.frame = [UIScreen mainScreen].applicationFrame;
[myScrollview setContentSize:[babe size]];
[myScrollview setMaximumZoomScale:2.0];
// Work out a nice minimum zoom for the image - if it's smaller than the ScrollView then 1.0x zoom otherwise a scaled down zoom so it fits in the ScrollView entirely when zoomed out
CGSize imageSize = babeView.image.size;
CGSize scrollSize = myScrollview.frame.size;
CGFloat widthRatio = scrollSize.width / imageSize.width;
CGFloat heightRatio = scrollSize.height / imageSize.height;
CGFloat minimumZoom = MIN(1.0, (widthRatio > heightRatio) ? heightRatio : widthRatio);

[myScrollview setMinimumZoomScale:minimumZoom];
[myScrollview setZoomScale:minimumZoom];

我的 UIBabeScrollView 被子类化以重载它的 layoutSubviews 像这样......

- (void)layoutSubviews {
[super layoutSubviews];

// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = ((UIImageView*)[self.subviews objectAtIndex:0]).frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width)
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
    frameToCenter.origin.x = 0;

// center vertically
if (frameToCenter.size.height < boundsSize.height)
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
    frameToCenter.origin.y = 0;

((UIImageView*)[self.subviews objectAtIndex:0]).frame = frameToCenter;

}

我想要的效果是图像始终居中,并且不能缩小超过图像的宽度或高度。

目前这在纵向模式下工作正常,但当切换到横向时,缩放比例不正确。

任何帮助将不胜感激,因为我仍然是一个初出茅庐的 iPhone 应用程序开发人员。

【问题讨论】:

    标签: iphone objective-c uiscrollview uiimageview centering


    【解决方案1】:

    在您的视图控制器中,实现-willRotateToInterfaceOrientation:duration:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
      if (toInterfaceOrientation == UIDeviceOrientationLandscapeLeft ||
          toInterfaceOrientation == UIDeviceOrientationLandscapeRight) {    
        [myScrollview setMinimumZoomScale:yourLandscapeMinimumZoomValue];
        [myScrollview setZoomScale:yourLandscapeMinimumZoomValue];
      } else {
        [myScrollview setMinimumZoomScale:yourPortraitMinimumZoomValue];
        [myScrollview setZoomScale:yourPortraitMinimumZoomValue];
      }
    }
    

    【讨论】:

    • 成功了,我自己也应该意识到这一点>。
    【解决方案2】:

    还有一点,仅供参考。我正在尝试 [scrollview setZoomScale: :animated] 方法。在方向更改时,它不会缩放到所需的值。

    这会导致动画未完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 2016-08-16
      相关资源
      最近更新 更多