Rox
Rox

Reputation: 909

Changing the current page of UIPageControl

I have a UIScrollView, in which I have added another 20 UIScrollView as subview. After that on 20 UIScrollView, I have added 4 UIImageView as their subview. All things are happening programmatically. Now I want to use UIPageControl for UIImageView which will change its current page value when I scroll the images.

 UIScrollView
 |
 |_ _ _ ScrollView Sub1---- PageControl 1
 |      |
 |      |_ _ _ Image 1
 |      |_ _ _ Image 2
 |      |_ _ _ Image 3
 |      |_ _ _ Image 4
 |
 |_ _ _ ScrollView Sub2 ---- PageControl 2
 |      |
 |      |_ _ _ Image 1
 |      |_ _ _ Image 2 
 |      |_ _ _ Image 3
 |      |_ _ _ Image 4
 |_ _ _ 
 | 
 |_ _ _
 |
 |_ _ _ ScrollView Sub20 ---- PageControl 20
 |      |_ _ _ Image 1
 |      |_ _ _ Image 2
 |      |_ _ _ Image 3
 |      |_ _ _ Image 4
 |

Now my question is: 1)Should I have to add 20 UIPageControl programmatically or just use only one which can handle all ScrollViews?

2) How can I handle current page value of different scroll Views? Because during scrolling of images, I can also scroll the UIScrollView.

PS: Number of UIScrollView and UIImageView may be vary. Above I mentioned maximum values.

Upvotes: 1

Views: 2803

Answers (1)

GenieWanted
GenieWanted

Reputation: 4513

You will need to playaround with the height of your MainScrollView (the one that holds all the UIScrollView subviews) and make use of - (void)scrollViewDidScroll:(UIScrollView *)scrollView method from UIScrollView delegate. The way it works is getting the current y position of my MainScrollView contentOffset and divide it by the height of my MainScrollView. Below code might give you some idea: (Considering that you have a vertical UIScrollView)

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if(scrollView == MAINS_SCROLLVIEW)
{
        CGFloat pageHeight = MAIN_SCROLLVIEW.frame.size.height; // getting the height

        int pageNo = round(formScrollView.contentOffset.y / pageHeight); // getting the page Number 
        pageControl.currentPage = pageNo; // Setting the page Number to pageControl
        NSLog(@"%i", pageNo);
        }
}

enter image description here

The above image is my view. I have three controls.. pageControl has been added inside the MainView and not in the UIScrollVIew.

Upvotes: 2

Related Questions