Iphone 如何更改方向更改时的最小缩放比例?

Iphone 如何更改方向更改时的最小缩放比例?,iphone,objective-c,uiscrollview,uiimageview,centering,Iphone,Objective C,Uiscrollview,Uiimageview,Centering,我正在尝试设置一个与iPhone上的照片应用程序非常相似的应用程序。我遇到的问题是,如果当前缩放比例小于最小值,我无法找到设置最小缩放比例并强制当前缩放比例返回最小值的好方法 下面是我目前如何设置scrollview - (void)viewDidLoad{ NSData * imageData = [[[NSData alloc] autorelease] initWithContentsOfURL: [NSURL URLWithString: imageURL]]; UIImage *ba

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

下面是我目前如何设置scrollview

- (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被子类化以重载它的布局子视图,如下所示

- (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;
-(无效)布局子视图{
[超级布局子视图];
//当图像小于屏幕尺寸时,将图像居中
CGSize boundsSize=self.bounds.size;
CGRect frameToCenter=((UIImageView*)[self.subviews objectAtIndex:0]).frame;
//水平居中
if(frameToCenter.size.width
}

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

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


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

在您的视图控制器中,实现
-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];
  }
}

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


它会导致一个未完成的动画。

这就成功了,我真的应该意识到这一点><谢谢!