Start ScrollView Somewhere Else Besides First Image - ios

Hi I am looking to see if I can take my scroll view which I have created below and start the scroll view on the 3rd or 4th image rather than start the scroll view from the beginning. Basically I dont want it to start on the first image but still be able to scroll backwards to get to them. Is this possible? Thank you!
int PageCount = 34;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"4miley.png",#"4BP1-2.png",#"S2-4.png",#"D3-4.png",#"4BP1-1.png",#"F2-4.png",#"B6-4.png",#"D6-4.png",#"4BP1-3.png",#"S1-4.png",#"B7-4.png", #"D2-4.png",#"F7-4.png",#"B1-4.png",#"F3-4.png",#"4BP1-4.png",#"D5-4.png",#"S3-4.png",#"B2-4.png",#"F5-4.png",#"D4-4.png",#"S7-4.png",#"F1-4.png",#"B3-4.png",#"S4-4.png",#"F6-4.png",#"D1-4.png",#"4BP1-5.png",#"B4-4.png",#"S5-4.png",#"F4-4.png",#"D7-4.png",#"S6-4.png",#"B5-4.png",nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
[scroller setShowsHorizontalScrollIndicator:NO];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
scroller.delegate = self;
[self.view addSubview:scroller];
width=scroller.frame.size.width;
xPos=0;
for (int i=0; i<PageCount; i++)
{
ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos +=scroller.frame.size.width;
}

After initializing images in scrollview , You can use scrollRectToVisible method to display 3rd and 4th image, set the frame of 3rd image in this method.
[scroller scrollRectToVisible:frame animated:YES];
//frame = your 3rd image

Once you have calculated the x pos & y pos of your 3rd or 4th image, you can use
[scrollView setContentOffset:CGPointMake(x, y) animated:YES];
or
[scrollView scrollRectToVisible:rect animated:YES];//put the rect of your image

After initializing images in scrollview , You can use setContentOffset:animated: method to display 3rd or 4th image
[scroller setContentOffset:CGPointMake(3*320, 0) animated:YES];

Related

How to implement fling feature on an ImageView with Objective C?

I have an UIImageView and I want to implement fling feature, when users flings on the screen the page can go up or down quickly.
Now my thinks is add two UISwipeGesturesRecognizers, but I don't know how to do next, should I use an animation to do this, and how to calculate the animation distance and time?
Also I find other answers said no need gesture recognizer can use scroll view, I am totally confused, are there any sample I can learn?
You can do it like that :
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setPagingEnabled:YES];
[scrollView setAlwaysBounceVertical:YES];
NSArray *imagesArray = [NSArray arrayWithObjects:#"img1.png", #"img2.png", #"img3.png", nil];
for (int i = 0; i < [imagesArray count]; i++)
{
CGFloat xOrigin = i * scrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
[imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[scrollView addSubview:imageView];
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height*[imagesArray count])];

How to create UIScrollview inside UIScrollview programmatically

Please tell me how to set scrollview inside a scrollview like nested process.
The following code works partially.
int x=10;
int y=10;
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
y=y+95;
}
Now all I can see are 3 scrollviews and the others are hidden. How can I create main scroll to view so that the child scrollViews are not hidden?
You need to have an initial scrollView that you then put these scrollViews in.
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
Then change
[self.view addSubview:scrollview];
To
[mainScrollView addSubview: scrollView];
//I have created Two Scroll view programmatically this way
UIScrollView *scrollViewOuter = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
scrollViewOuter.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
scrollViewOuter.contentSize = CGSizeMake(2000.0f, 2000.0f);
UIScrollView *scrollViewInner = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
scrollViewInner.backgroundColor = [UIColor whiteColor];
scrollViewInner.contentSize = CGSizeMake(2000.0f, 2000.0f);
[scrollViewOuter addSubview:scrollViewInner];
[self.window addSubview:scrollViewOuter];
//You can change frame and use in your own way
Just create a parent scrollview that is big enough to hold the 5 smaller ones, then change this line:
[self.view addSubview:scrollview];
to
[parentScrollView addSubview:scrollview];

How to enable zoom for two different UIScrollview in single UIView?

I am displaying two images on single view where both images must have zoom functionality.My page height is around 1000 pixels and images must be display on top and bottom of page. For that I have added two different scroll view and added image views on scroll views.
Now Zoom is working properly for second image .But first image is getting displayed in top left corner of scroll view once zoomed out. It seems there are two images.
Here is the delegate method i am using:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return [[scrollView subviews] objectAtIndex:0];
}
Is there anything i am missing ? or is this not really possible to enable zoom when there are two scroll views for single page.
Here is the code its working for enable zoom when there are two scroll views for single page.
//Create First ImageView
image1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 220)];
image1.image = [UIImage imageNamed:#"image1.png"];
image1.contentMode = UIViewContentModeScaleToFill;
//Create and Set Values for Scrollview 1
scroll1 =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 220)];
scroll1.contentSize = image1.frame.size;
[scroll1 setMinimumZoomScale:1.0];
[scroll1 setMaximumZoomScale:4.0];
scroll1.delegate = self;
scroll1.clipsToBounds = YES;
scroll1.tag =0;
[scroll1 addSubview:image1];
image1.userInteractionEnabled =YES;
scroll1.zoomScale = 1;
[self.view addSubview:scroll1];
Second Scrollview and ImageView
// Create Second ImageView
image2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 260)];
image2.image = [UIImage imageNamed:#"image2.png"];
image2.contentMode = UIViewContentModeScaleToFill;
// Create Second ScrollView
scroll2 =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 220, 320, 260)];
scroll2.contentSize = image2.frame.size;
[scroll2 setMinimumZoomScale:1.0];
[scroll2 setMaximumZoomScale:4.0];
scroll2.delegate = self;
scroll2.clipsToBounds = YES;
scroll2.tag =1;
[scroll2 addSubview:image2];
image2.userInteractionEnabled =YES;
scroll2.zoomScale = 1;
[self.view addSubview:scroll2];
This will help you, since it is already working for me.
Delegate method as follow:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if (scrollView.tag ==0) {
return image1;
}
else return image2;
}

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Ipad tableview and imageview

How to insert four tableview in a scrollview by pagination.i know how to do this ,but i need a image view as first page and then tableview as second,third and forth page.
Thankz in advance
_scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 55, 1000, 460)];
_scroll.backgroundColor=[UIColor clearColor];
_scroll.pagingEnabled=YES;
_scroll.contentSize = CGSizeMake(1000*5, 460);
[_scroll setBackgroundColor:[UIColor lightGrayColor]];
CGFloat x=0;
for(int i=1;i<6;i++)
{
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(x+10, 10, 750, 440) style:UITableViewStylePlain];
[table setBackgroundColor:[UIColor whiteColor]];
table.rowHeight = 120;
table.separatorColor = [UIColor grayColor];
table.delegate=self;
//table.dataSource=self;
[_scroll addSubview:table];
[table release];
x+=1000;
}
[self.view addSubview:_scroll];
_scroll.showsHorizontalScrollIndicator=NO;
[_scroll release];
Ignoring the recommendation that UITableViews are not embedded inside scroll views as they are scrollviews in themself. So dont do this (not a joke).
You add the imageview with addSubview: just like the tableviews you want add. They are all UIViews and are all treated the same way.
So for a hypothetical scrollview with size (100,100) and imageview/tableviews of size (100,100)
imageview.frame = CGRectMake(0,0,100,100);
[scrollview addSubview:imageview];
tableview1.frame = CGRectMake(100,0,100,100);
[scrollview addSubview:tableview1];
tableview2.frame = CGRectMake(200,0,100,100);
[scrollview addSubview:tableview2];
tableview3.frame = CGRectMake(300,0,100,100);
[scrollview addSubview:tableview3];
scrollview.contentSize = CGSizeMake(400,100);
scrollview.pagingEnabled = YES;

Resources