I am new to iPhone developer,
I want scrolling in my Image,
Here is my code snippet,
- (void)viewDidLoad {
UIImage *image1 = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]];
self.imageView = [[UIImageView alloc] initWithImage:image1];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image1.size};
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
[self centerScrollViewContents];
}
- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the view that you want to zoom
return self.imageView;
}
also bg.png is not getting applied to my background.
Any help will be appreciated.
the answer is actually a combination of many of the other answers & comments, plus some other adjustments.
viewForZoomingInScrollView: delegate implementation won’t get called.
i’ve provided this call in viewDidLoad: , though you can also hook this
up in interface builder or storyboard.
original code, you are setting your
.imageView.sizeand your.scrollView.contentsSizeto exactly the same thing. you don’t needthe code that sets the
.imageView.frame.that will happen as part ofinitWithImage:. and this will make the sizes different..scrollView.contentSizeto theimage.size.it sort of matters where ou do this, at least in terms of that it must
happen prior to setting the zoom scale.
can set this in storyboard or interface builder.
.userInteractionEnabled = YES, thoughgenerally, these are the defaults that get set in storyboard/Interface Builder,
so i’m not going to include them in the code changes below.
centerScrollViewContentsisn’t really centering the contentsas much as it is re-shaping the imageView frame. i’ve simply commented out
the call, because i think it’s completely unnecessary. instead, i’ve made
a simple call to start your image in the upper left corner.
zoomScale,minimumZoomScaleandmaximumZoomScaleis not necessary. if you also want to pinch-zoom, thenyou will need to set all three of these, and in the proper places.
bounds. if this is truly a concern (i.e. if you know your bg.png is so
small that it needs to be stretched), then calculate the contentFrame to
image frame width ratio and height ratio separately, and whichever of
these is larger will be your new zoomScale, and you’ll be able to scroll
in the other direction. or, there are other ways to decide how you want
that scale set. search stackoverflow.com for other answers for setting
the zoom scale as desired.
here’s the code after applying these items.