开发者

How do I Change minimum Zoom scale on orientation change?

I am trying to set up an app very similar to the Photos app on the iPhone. The problem I am running into is that I cannot figure out a good way to set the minimum zoom scale and force the current zoom scale back to the minimum if it is currently smaller than the minimum.

Here is how I am currently setting up my 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];

My UIBabeScrollView is subclassed to overload it's layoutSubviews like so...

- (void)layoutSubviews {
[super layoutSubviews];

// center the image as it becom开发者_StackOverflow中文版es 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;

}

The affect I am going for is that the image is always centered, and cannot be zoomed out more than the width or height of the image.

Right now this works fine in portrait mode, but when it is switched to landscape, the zoom scales are incorrect.

Any help would be appreciated, as I am still a fledgling iphone app developer.


In your view controller, implement -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];
  }
}


Also another point, just for information. I was trying the [scrollview setZoomScale: :animated] method. On orientation change, it would not Scale to the required value.

It results in an uncompleted animation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜