Reputation: 6490
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.
Upvotes: 5
Views: 1828
Reputation: 869
scrolling the images
scroll.userinteractionEnable = YES;
and other then,
self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"image.png"]]];
Upvotes: 0
Reputation: 7563
the answer is actually a combination of many of the other answers & comments, plus some other adjustments.
.imageView.size
and your
.scrollView.contentsSize
to exactly the same thing. you don't need
the code that sets the .imageView.frame.
that will happen as part of
initWithImage:
. and this will make the sizes different..scrollView.contentSize
to the image.size
.
it sort of matters where ou do this, at least in terms of that it must
happen prior to setting the zoom scale..userInteractionEnabled = YES
, though
generally, these are the defaults that get set in storyboard/Interface Builder,
so i'm not going to include them in the code changes below.centerScrollViewContents
isn't really centering the contents
as 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
, minimumZoomScale
and
maximumZoomScale
is not necessary. if you also want to pinch-zoom, then
you will need to set all three of these, and in the proper places.here's the code after applying these items.
(void)viewDidLoad {
self.scrollView.delegate = self; // can be set in storyboard
self.scrollView.scrollingEnabled = YES; // can be set in storyboard
NSString* bgPng
= [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"];
UIImage *image1 = [UIImage imageWithContentsOfFile:bgPng];
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;
// set zoom scale here if desired.
// if zoom is implemented, the following call should be made
// after setting .zoomScale, .minimumZoomScale & .maximumZoomScale
self.scrollView.contentOffset = CGPointZero;
// [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;
}
Upvotes: 4
Reputation: 106
Did you set the scrollview delegate somewhere else?
Like this: self.scrollView.delegate = self;
Upvotes: 0
Reputation: 501
In the code above, you need to set the minimum and maximum zoom scale.
self.scrollView.minimumZoomScale=0.5; self.scrollView.maximumZoomScale=6.0;
Upvotes: 0
Reputation: 4789
First of all check your scrollview frame . Scrolling will be enabled only when frame size is < contentsize. Because if all the content is visible in frame then why do you need scrolling at all ??
try this :
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; //init with whatever frame you have scroll.scrollEnabled = YES // this is imp ... don't ignore this //whatever your code is self.scrollView.contentSize = image.size;// this is wrong //contentSize property to the size of the scrollable content. This specifies the size of the scrollable area. // do this self.scrollView.contentSize = imageView.frame.size;
Upvotes: 1
Reputation: 1930
In the above code, you have provided
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;
The content size of scrollView which you are setting is equal to the size of the image i.e the above bolded code. Try with the following code.
CGSize sizeOfScroll = image.size;
sizeOfScroll.width *= 1.5f;
sizeOfScroll.height *= 1.5f;
self.scrollView.contentSize = sizeOfScroll;
Upvotes: 0
Reputation: 107231
Set the user interaction enabled property of image view and scroll view to yes. Please check with this code,
- (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};
imageView.userInteractionEnabled = YES;
scrollView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
[self centerScrollViewContents];
}
And if you need to set the image as your scroll view's background, use this:self.scrollView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bg.png"]]];
Upvotes: 0
Reputation: 2032
well, try this :
UIImageView *tempImgv = [[[UIImageView alloc]init] autorelease];
[tempImgv setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/bg.png", [[NSBundle mainBundle]resourcePath]]]];
[tempImgv setFrame:your_desired_frame];
self.imageView = tempImgv;
[self.scrollWiew setContentSize: size_greater_than_scroll_frame];
[self.scrollWiew addSubview:self.imageView];
Upvotes: 1