How can I setup two UIScrollViews that have nested UIImageViews? - iphone

Ok we all know that nesting an image view inside of a scroll view allows you to have an image that you can pinch and zoom. Pretty cool. I want to duplicate this (eventually quadruple) feature.
In IB I have setup my 2 scroll views side by side, and have nested imageViews inside of them and have hooked them up to the proper variables and everything.
Inside of my viewController viewDidLoad I do this:
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"monitor.JPG"]];
[scrollView addSubview:imageView3];
//[scrollView setFrame:CGRectMake(0, 0, 1024, 660)];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 5;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
imageView31 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"monitor.JPG"]];
[scrollView1 addSubview:imageView3];
It seems like this should run ok. But is what happens when I build in run is quite unusual. There is just a blank background for where my first scroll view should be and then the second scrollView has its image in there...
If I just do the code for the first scrollView, I get the image. So it's like I am somehow overwriting the first by including the second?
I really can't understand how or why this behavior is occurring. Maybe I am going about it all wrong, what am I missing? Does anyone have any ideas as to how to implement this multiple scrollView view?
Thanks

[scrollView addSubview:imageView3];
[scrollView1 addSubview:imageView3];
The second one should be imageView31

Related

Adding UIScrollView to view and sending it to the back

I have added few UI components using Storyboard (UILabel, UIImageView). I am trying to put UIScrollView in this view, below all other elements:
Here is my code right at the end of -viewWillAppear method (code from my View Controller):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
The reason why I need a UIScrollView is large amount of text in detailsLabel - I'm attaching code I use for handling this label programatically:
detailsLabel.text = #"Some long text taken from databse";
[detailsLabel sizeToFit];
detailsLabel.numberOfLines = 0;
detailsLabel.lineBreakMode = UILineBreakModeWordWrap;
What I am doing wrong? How do I put UIScrollView below all other elements of my main view?
Try this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view insertSubview:scroll atIndex:0];
...
[self.view insertSubview:detailsLabel atIndex:1];
Put your code in ViewDidLoad . Then add the labels after you have added the UIScrollView to the self.View in the ViewDidLoad itself. So by default the UIScrollView would be added on the self.View and then your label. Now you can either add the label on the scrollview or onto the self.view .
I've followed the instructions in this answer and it worked great: I've changed the Custom Class in Identity Inspector from UIView to UIScrollView and added this line to my -viewDidLoad in the View Controller file:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 700)];
Option One:
Declare detailsLabel in .h:
IBOutlet UILabel * detailsLabel; // MAP IT WITH NIB or STORYBOARD
give it property and Synthesise in .m.
here in viewDidLoad declare:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, 480);
scroll.showsVerticalScrollIndicator = YES;
[self.view addSubview:scroll];
[self.view sendSubviewToBack:scroll];
[scroll addSubview:detailsLabel];
// IT SHOULD WORK.
Option two:
If you have large amount of text to display, then you can also use UITextView, it has inbuilt scrollview. And you can also set its text edit property.

The right way to implement loadView?

I have a question regarding the implementation of loadView:
Right now, I have it like this:
- (void)loadView
{
image = [UIImage imageNamed:#"plan.gif"];
scrollView=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,scrollView.frame.size.width + 40, scrollView.frame.size.height)];
imageView.image = image;
[imageView setContentMode:UIViewContentModeCenter];
scrollView.contentSize = image.size;
scrollView.maximumZoomScale = 3.0;
scrollView.bounces = NO;
scrollView.delegate = self;
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
[scrollView addSubview:imageView];
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}
I suppose some of it should be in viewDidLoad:?
Thanks in advance.
This seems fine to me.
viewDidLoad is normally used as your hook after getting a view returned from IB. In this case you are essentially just doing the work of IB in code (setting up the view heirachy and configuring it).
Therefore in this case I think splitting out the logic may be superfluous unless it makes your code more readable.

Dynamically adding a scroll view above the view iphone

I have created a UIView through code and want to add some images and description on that view.For that purpose i wished to add a scroll view so that we can user can scroll down to see the images and description.Can anyone help me how to add a scroll view above a UIView through code.Thanks in advance.
Hi try this:-
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[scrollView setDelegate:self];
[scrollView setShowsVerticalScrollIndicator:NO];
[scrollView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:scrollView];
Now set the content size of scroll view:-
scrollView.contentSize = CGSizeMake(320, 600);//put values according to your requirements.
UIScrollView *newView = [[UIScrollView alloc] initWithFrame:scrollFrame];
[newView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[newView setDelegate:self];
[newView setBackgroundColor:backgroundColor];
[newView setPagingEnabled:YES];
[newView setShowsVerticalScrollIndicator:NO];
[newView setShowsHorizontalScrollIndicator:NO];
[[self view] addSubview:newView];
you can set Frame In place of scrollFrame.
cheers
Actually you probably don't want to create a UIView, you should create a UIScrollView then add the images and that as subviews of the UIScrollView. Then you'll have everything scrolling. You'll then set up the UIScrollView the way you want it to be.

Zooming within Nested UIScrollViews

Its a familiar topic Im creating a graphic novel using UIScrollview. The plan is to be able to page along the novel with a top level uiscrollview. Within this are a series of nested uiscrollviews which have a uimageview embedded within each. The idea is that large images can be panned around and zoomed into but still paged along. I have most of this working but I cant get the zoom working. I have implemented the zoom delegate method but to no avail, zooming just pans the image around. I suspect this is something due to embedded nature of the nested scrollviews but I cant see what im missing. I had a look at the Apple scrollview suite example but couldnt find an answer.
This is what I have working so far:
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
scrollview.pagingEnabled = YES;
scrollview.scrollEnabled =YES;
scrollview.clipsToBounds = NO;
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollview.showsHorizontalScrollIndicator = YES;
scrollview.backgroundColor = [UIColor blackColor];
NSInteger viewcount=4;
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"mars.png"],[UIImage imageNamed:#"mercury.png"],[UIImage imageNamed:#"neptune.png"],[UIImage imageNamed:#"venus.png"],nil];
for (int i = 0; i <viewcount; i++)
{
CGFloat x = i * self.view.frame.size.width;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
[subView setBackgroundColor:[UIColor blackColor]];
[subView setCanCancelContentTouches:NO];
subView.clipsToBounds = NO; // default is NO, we want to restrict drawing within our scrollview
subView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
aImageView = [[UIImageView alloc ] initWithImage:[images objectAtIndex:i]];
[self.aImageView setTag:ZOOM_VIEW_TAG];
[subView addSubview:aImageView];
[subView setContentSize:CGSizeMake(aImageView.frame.size.width, subView.frame.size.height)];
subView.minimumZoomScale = 1;
subView.maximumZoomScale = 3;
subView.delegate = self;
[subView setScrollEnabled:YES];
subView.contentSize = aImageView.frame.size;
[scrollview addSubview:subView];
[subView release];
}
[self.view addSubview:scrollview];
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog (#"test");
UIView * view = nil;
view = [subView viewWithTag:ZOOM_VIEW_TAG];
return view;
}
Worked this out, for anyone else trying to do this.
One thing I missed a line of code out on the post above which is quite fundamental!, in the viewdidLoad method
just above [self.view addSubview:scrollview]; there should be the following which calculates the width of all of the subviews
scrollview.contentSize = CGSizeMake(self.view.frame.size.width*viewcount,self.view.frame.size.height);
The code in the -(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
method should be:
return [scrollView.subviews objectAtIndex:0];
This allows you to zoom and pan around images and also page along a set of images very nicely. Just got to work out how to zoom back to the original size automagically when moving on to the next image as its stays zoomed upon paging along..

how to scroll list of uiviews in a UIScrollView which will be in bottom of the page?

i want to place a list of uiviews instead of uiimageviews as a content in uiscrollview for my ipad app.i got images scrolling by using imageviews, but i need uiviews as a list of subviews that has to be scroll horizontally?i tried adding uiviews in uiscrollview through Interfacebuilder ,but while running the app i can see only one uiview where as all remaining views are not there. please help me out of this.
I tried it like that, still it's not showing the view, and also I tried programmatically for dimensions of UIScrollView and views inside it like the the code as below: –
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[subView2 release];
[scrollView release];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView1];
[subView1 release];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blueColor];
[scrollView addSubview:subView2];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
[subView2 release];
[scrollView release];
[scrollView setContentSize:CGSizeMake(758+758, 365+365)]]
you need to set the contentsize to be able to scroll and see all subviews.
You need to add UIView as an when needed. you need to use programmatic approach to add UIView. You should have a look at PageControl sample code provided by Apple. I hope you get your answer.
i tried with [758+758,365]; . now it is scrolling horizontally but only first view i can see. second view is not coming .
scrollView.frame=CGRectMake(0, 604, 728,365);
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
[scrollView setContentSize:CGSizeMake(1600,365)];
UIView *subView1=[[UIView alloc]initWithFrame:CGRectMake(20, 604, 758,365)];
subView1.backgroundColor=[UIColor blueColor];
subView1.frame=scrollView.bounds;
[subView1 addSubview:myImageView];
[scrollView addSubview:subView1];
UIView *subView2=[[UIView alloc]initWithFrame:CGRectMake(828, 604, 758,365)];
subView2.backgroundColor=[UIColor blackColor];
subView2.frame=scrollView.bounds;
[scrollView addSubview:subView2];
[subView1 release];
[subView2 release];
[scrollView release];