Wednesday, June 5, 2013

UIScrollView setZoomScale animated example in Objective C (iOS).

UIScrollView setZoomScale animated

A floating-point value that specifies the current zoom scale.

- (void)setZoomScale:(float)scale animated:(BOOL)animated

Parameters of [UIScrollView setZoomScale animated]
The new value to scale the content to.
YES to animate the transition to the new scale, NO to make the transition immediate.

Discussion of [UIScrollView setZoomScale animated]
The new scale value should be between the minimumZoomScale and the maximumZoomScale.

UIScrollView setZoomScale animated example.
A UIScrollView will not zoom unless it has its delegate property set to a valid UIScrollViewDelegate that responds to

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

-(void)viewDidAppear:(BOOL)animated {

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bigimage.jpg"]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.contentSize = imgView.frame.size;

    scrollView.minimumZoomScale =  scrollView.frame.size.width / scrollView.contentSize.width * 0.99;
    scrollView.maximumZoomScale =  2;

    [self.view addSubview:scrollView];
    scrollView.delegate = self;

    [scrollView addSubview:imgView];

    [scrollView setZoomScale:0.5 animated:YES];
    NSLog(@"current zoomScale: %f", scrollView.zoomScale);
    [imgView release];

Example of [UIScrollView setZoomScale animated].
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"bounds of current view");
    NSLog(@"widht: %f, height: %f", self.view.bounds.size.width,self.view.bounds.size.height);
    NSLog(@"size of the image");
    NSLog(@"widht: %f, height: %f", self.imageView.image.size.width,self.imageView.image.size.height);
    NSLog(@"size of the scrollView BEFORE resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);
    [self.scrollView setZoomScale:0.5 animated:NO];
    NSLog(@"size of the scrollView AFTER resizing");
    NSLog(@"widht: %f, height: %f", self.scrollView.contentSize.width,self.scrollView.contentSize.height);

UIScrollView setZoomScale animated example.
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

CGSize screenSize = [[self view] bounds].size;

if (myScrollView.zoomScale <= initialZoom +0.01) //This resolves a problem with the code not working correctly when zooming all the way out.
imageView.frame = [[self view] bounds];
[myScrollView setZoomScale:myScrollView.zoomScale +0.01];

if (myScrollView.zoomScale > initialZoom)
if (CGImageGetWidth(temporaryImage.CGImage) > CGImageGetHeight(temporaryImage.CGImage)) //If the image is wider than tall, do the following...
if (screenSize.height >= CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is greater than the zoomed height of the image do the following...
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), 368);
if (screenSize.height < CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the height of the screen is less than the zoomed height of the image do the following...
imageView.frame = CGRectMake(0, 0, 320*(myScrollView.zoomScale), CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale]);
if (CGImageGetWidth(temporaryImage.CGImage) < CGImageGetHeight(temporaryImage.CGImage)) //If the image is taller than wide, do the following...
CGFloat portraitHeight;
if (CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale] < 368)
{ portraitHeight = 368;}
else {portraitHeight = CGImageGetHeight(temporaryImage.CGImage) * [myScrollView zoomScale];}

if (screenSize.width >= CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is greater than the zoomed width of the image do the following...
imageView.frame = CGRectMake(0, 0, 320, portraitHeight);
if (screenSize.width < CGImageGetWidth (temporaryImage.CGImage) * [myScrollView zoomScale]) //If the width of the screen is less than the zoomed width of the image do the following...
imageView.frame = CGRectMake(0, 0, CGImageGetWidth(temporaryImage.CGImage) * [myScrollView zoomScale], portraitHeight);
[myScrollView setZoomScale:myScrollView.zoomScale -0.01];

End of UIScrollView setZoomScale animated example article.