Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

boyplunder

macrumors regular
Original poster
Sep 8, 2008
165
0
UK
Hi All,

Haven't been on here recently, but would like a little help in managing an image in a scrollview.

I have the following code:
Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];
	self.imageView = tempImageView;
	[tempImageView release];
	
	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
	scrollView.maximumZoomScale = 1;
	scrollView.minimumZoomScale = 0.5;
	scrollView.clipsToBounds = YES;
	scrollView.delegate = self;
	[scrollView addSubview:imageView];
}

What I'm trying to figure out is a way I can centre the image when the window first appears. IB allows you to offset the image in the view, but this is permanent, which is not what I want. ONce the person moves the image or zooms in or out, it can stay where it is. [I'm feeling this is more complicated than I have done before, but willing to learn, if that's the case.]

Any help is much appreciated.
 
Not really explained too well there.

The code shown is the initial set up for the view. This works fine. Since then I have been trying to see if I can get the image to start off centred when the user opens the app. Not a huge amount of success so far, and wondered if I could ask for a few suggestions in doing this or the best approach.

I have learnt through doing and by example, and not a deep programmer like some of you. Any guidance would be appreciated.
 
Have you checked out UIScrollView's contentOffset property?

Thanks Dejo, that was the baby!
Actually, it led me onto a number of things I will be needing that were not immediately obvious.

PhoneyDeveloper, I have been working with the example you mentioned, and it has been a great help in starting off.
 
Hi All,

I wanted to post the code that I ended up with after the help I got. There were a couple of other issues that led on from solving the image position problem I posted.

The issues were: The starting size of the image, the offset position and the compensation in max and min zoom given that I had reduced the logical size of the graphic.

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];
	self.imageView = tempImageView;
	[tempImageView release];
	
	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
	[scrollView setContentOffset:CGPointMake(470.0f, 200.0f)];
	scrollView.maximumZoomScale = 3;
	scrollView.minimumZoomScale = 1;
	scrollView.clipsToBounds = YES;
	scrollView.delegate = self;
	[scrollView addSubview:imageView];
	
	scrollView.contentSize = CGSizeMake(imageView.image.size.width*0.32, 
										imageView.image.size.height*0.32 );
	
	imageView.frame = CGRectMake(imageView.frame.origin.x, 
								 imageView.frame.origin.y, 
								 imageView.frame.size.width*0.32, 
								 imageView.frame.size.height*0.32);
	
}

This done, I do have a few things to address, but the help was great.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.