Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIImageView in UIScrollView

MichafMichaf Posts: 6New Users
edited March 2010 in iOS SDK Development
Ok, this is something that's been hanging in my code for a while and I just can't get it to work. I just need my UIImageView to get centered in a UIScrollView and zoom working properly. The Problem is the centering, either you set the Frame of your UIImageView to the size of your UIScrollView and the frame being too large the edge of the image bounces off the image, or you set the UIImageView to the frame of the enclosing UIImage but then the UIImageView is placed on the top of the UIScrollView's coordinates. I've tried with .center .contentSize .contentOffSet/inset, the most promizing beeing UIImageView.center = UIScrollView.center. The problem is after the zoom the height of the contentSize of the UIScrollView is plain wrong, exceeding the new size of the UIImageView. I've set an xcode project covering this topic at .Mac - iDisk
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIImage* image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Vespa" ofType:@"jpg"]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor blackColor];
window.backgroundColor = [UIColor blackColor];
// I'm setting the UIImageView size so it fills the screen first
float ratio = image.size.width/[[UIScreen mainScreen] bounds].size.width;
CGRect imageFrame = CGRectMake(0,0, [[UIScreen mainScreen] bounds].size.width, (image.size.height/ratio));
imageView.frame = imageFrame;
//
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
//imageView.center = scrollView.center; This is one of my many attempts, try this, the actual size of the scrollView's content view after the zoom gets a wrong height
[image release];
[imageView release];
[window addSubview:scrollView];
    [window makeKeyAndVisible];
}

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

return imageView;
}
Post edited by Michaf on
· ·

Replies

  • brendandbrendand Posts: 223Registered Users
    edited November 2008
    Better late than never I see here... Old post, but this is what I did to get your example to work:
    // commented out this code as it's not needed. The contentMode of UIViewContentModeScaleAspectFit will make it fit in its initial state.
    
    //	float ratio = image.size.width/[[UIScreen mainScreen] bounds].size.width;
    //	CGRect imageFrame = CGRectMake(0,0, [[UIScreen mainScreen] bounds].size.width, (image.size.height/ratio));
    //	imageView.frame = imageFrame;
    
    [scrollView setContentSize: CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height)];
    

    Then pinching and panning work fine.
    · ·
  • brendandbrendand Posts: 223Registered Users
    edited November 2008
    Now if someone could modify this code to get double-tap zoom and un-zoom to work, that'd be great :-)
    · ·
  • puppetpuppet Posts: 11Registered Users
    edited March 2009
    - (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    	CGFloat tempx = view.center.x-160;
    	CGFloat tempy = view.center.y-160;
    	myScrollViewOffset = CGPointMake(tempx,tempy);
    }
    

    where 160 is the width/height of my scrollview

    then later I:
    myScrollView.contentOffSet = myScrollViewOffset;
    
    · ·
  • mcamilomcamilo Posts: 8New Users
    edited August 2009
    In which method do you set the contentOffSet to myScrollViewOffset ?
    · ·
  • mrburns05mrburns05 Posts: 26Registered Users
    edited March 2010
    :p
    brendand wrote: »
    Now if someone could modify this code to get double-tap zoom and un-zoom to work, that'd be great :-)

    what you want is in apples scrollviewsuite

    I forget which sample but there's a class in there called "TapDetectingImageView"

    just drop the class files in your project and use that as an imageView subclass, grab it's delegate methods you'll see also in the project and pit em in yours

    from there on you can do as you please with 1&2 finger taps, double taps and add more than that if you wish. Good luck
    · ·
Sign In or Register to comment.