I have 5 pages and I want to start on the 3rd page. I have added some code to skip to the 3rd page
CGRect frame = self.predictionsScroll.frame;
frame.origin.x = frame.size.width * (pagesBefore);
frame.origin.y = 0;
[self.predictionsScroll scrollRectToVisible:frame animated:YES];
This works perfectly except the pageControl still shows that it is on the first page. If I drag the view a tiny bit it updates to where it is, but this should start on the middle dot. Here is my method for updating the pageControl page.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView == self.predictionsScroll) {
CGFloat pageWidth = self.predictionsScroll.frame.size.width;
float fractionalPage = self.predictionsScroll.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pagecontrol.currentPage=page;
}
}
I have tried adding a manuel pagecontrol.currentPage = pagesBefore; But this does not seem to work.
Related
Create a Scrollview add UIPagecontrol and load the array values successfully. Put timer for scroll it automatically. Want to show after complete the last array value it want to move the first array value automatically.(Now my code working like get reverse and move to first array value). I need go automatically to first. Help me.
Scrollview page move code
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int width;
width = ScrollViewPage.frame.size.width;
float xPos = scrollView.contentOffset.x+10;
pageControl.currentPage = (int)xPos/width;
}
///Add timer for move automatically using tis code
- (void)loadNextController
{
pageControl.currentPage = (pageControl.currentPage+1)%self->pageControllMutableImageArray.count;
[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES];
}
///UIpagecontrol dot button click target action code
- (IBAction)pageTurn:(id)sender
{
NSUInteger page = pageControl.currentPage;
CGRect frame = ScrollViewPage.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[ScrollViewPage scrollRectToVisible:frame animated:YES];
}
How it possible in my code. help me thanks advance.
You can do something like this,
- (void)loadNextController
{
//put if statement here to check whether it is page object something like,
//have aasume that your first page number is 0. so take count - 1 in if
if(pageControl.currentPage == (pageControllMutableImageArray.count - 1)){
//start from initial. set current page = 0 and contentoffset of first page
}
else {
// your code for next page may be like,
pageControl.currentPage = (pageControl.currentPage+1)%self->pageControllMutableImageArray.count;
[ScrollViewPage setContentOffset:CGPointMake(pageControl.currentPage * self.view.bounds.size.width, 0) animated:YES];
}
}
Hope this will help. :)
Update 2 :
you can set content offset of first page like:
CGPoint newOffset = CGPointMake(0, 0);
[self.standardScrollView setContentOffset:newOffset animated:YES];
My problem is: The scrollview has 5 pages, when the global value of current page is 2, I swipe to left or right, it always return to first page. I cannot change next/previous page with the current page. This is my code:
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl_Portrait.currentPage = page;
[self loadDataInChartView:page];
}
Please give me some advice. Thanks in advance.
You need to change the page control only after the UIScrollview has ended scrolling
Make sure paging is enabled for best results
use
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl_Portrait.currentPage = page;
}
I've found a good solution for looping my scrollview, but I'm also using a UIPageControl for showing what the actual page's number is.
I change the pageControl.currentPage property when the scrollview scrolls, so if you scroll the scrollview more than a half the pageControl shows the new page.
I'd like to keep this feature, but with my actual code the pageNumber is not correct.
I think I should use this: https://github.com/gblancogarcia/GBInfiniteScrollView
But I can't make it work...
Here's my code:
-(void)rotateIfNecessary{
if(scrollView.contentOffset.x == 0) {
CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsRight];
}
else if(scrollView.contentOffset.x == (scrollView.contentSize.width - scrollView.frame.size.width) ) {
CGPoint newOffset = CGPointMake(scrollView.contentOffset.x-scrollView.bounds.size.width, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsLeft];
}
}
-(void)rotateViewsRight {
NSMutableArray* controllers = [DashboardFrameUtil getFrameViewControllers];
DashboardFrameViewController *endView = [controllers lastObject];
[controllers removeLastObject];
[controllers insertObject:endView atIndex:0];
[self resizePages];
}
-(void)rotateViewsLeft {
NSMutableArray* controllers = [DashboardFrameUtil getFrameViewControllers];
DashboardFrameViewController *endView = [controllers firstObject];
[controllers removeObjectAtIndex:0];
[controllers addObject:endView];
[self resizePages];
}
Then I call rotateIfNecessary in my scrollViewDidEndDecelerating, everything works fine, but the page calculating.. (when the scrollview resizes itself, it calls the scrollViewDidScroll where I calculate the pageNumber:
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
The pageControl doesn't show the correct page number when the scrollView loops.
Any idea?
Thank you in advance!
Let's start with:
if(scrollView.contentOffset.x == 0) {
CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
...
}
condition is true when scrollView.contentOffset.x == 0, so what for do you add scrollView.contentOffset.x (it's surely = 0) to scrollView.bounds.size.width ?
UPDATED
I didn't run code - there can be some mistakes
If I understand correctly when you, for example, rotate right - last element became first and you want LAST page to be showen?
If yes -- the problem with pages is because you change contentOffset all the time and also change 'data source' of scroll view, so there's no correlation between contentOffset and individual DashboardFrameViewController - same DashboardFrameViewController can be at different offsets in different moments of time. So You have to calculate page N according to something else.
For example:
Somewhere in ivar store initial controllers (with initial order):
- (void)viewDidLoad {
[super viewDidLoad];
_initialControllers = [[DashboardFrameUtil getFrameViewControllers] copy];
}
then you need some method to discover what vc is currently vissible or nearest to center (and store in ivar _currentlyVissibleVC), for example:
- (DashboardFrameViewController *)vissibleVC {
for (DashboardFrameViewController *vc in _initialControllers){
if (CGRectContainsPoint(vc.frame, CGPointMake(scrollView.contentOffset.x + scrollView.bounds.size.width/2, scrollView.contentOffset)){
_currentlyVissibleVC = vc;
return;
}
}
}
then you can calculate page num:
int pageN = [_initialControllers indexOfObject:_currentlyVissibleVC] + 1;
Hope helps
In the following picture, how do you have Multi-option picker and the horizontal scroller in the same white rounded rectangle? It looks great for sorting, and I'd love to implement something like that.
You need to have a UITableView with two sections (one named "section 1" and one named "section 2").
Each section has a single row, and each row's "cell" contains an option picker inside it's "Accessory View".
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
That option picker is not part of the system's library of controls, I don't know where it came from.
You could do the option picker as a UIScrollView, attaching a UIPageControl to it, inside a UIView, with two UIViews (or UIImageViews) providing the transparent fading effect. You add n-views to the scrollview, each with the same height/width. If you want the snap, which I imagine you will, track the current page with the UIScrollViewDelegates like so:
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float pageWidth = scrollview.frame.size.width;
int page = floor((scrollview.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
[self scrollview:scrollview scrollToPage: page];
}
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if( decelerate == FALSE )
{
float pageWidth = scrollview.frame.size.width;
int page = floor((scrollview.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
[self scrollview:scrollview scrollToPage: page];
}
}
And the scrollview:scrollToPage: method will give you the snap like so:
-(void) scrollview:(UIScrollView*) scrollview scrollToPage:(NSInteger) page
{
[_myPageControl setCurrentPage:page];
CGRect frame = scrollview.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollview scrollRectToVisible:frame animated:YES];
}
I've got a UIScrollView containing several tables, and a UIPageControl keeping track of which page the user is currently viewing. It works fine UNTIL i start scrolling up on the tableview, at which point the UIPageControl adjusts and says (incorrectly) that we are on the first page.
Here is my code:
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView {
if (pageControlIsChangingPage) {
return;
}
/*
* We switch page at 50% across
*/
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
and then this as well:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
pageControlIsChangingPage = NO;
}
Not sure what exactly is causing this to happen . . . any ideas? Im stumped :(
I believe the issue was that both the UITableView and the UIScrollView, since they both had the delegates set to self, were calling the same scrollview methods. I added a tag to the main scrollview, and then accounted for it by performing a check on the Tag when adjusting the page, like so
if (_scrollView.tag == 100) {
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
finally
When you scroll the page, does it scrolls correctly?
if (pageControlIsChangingPage)
return;
It seems when you do not change page (i.e. you scroll the tableview instead), return'll not be executed. And so
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
part will be executed, at this moment, _scrollView.contentOffset.x will less than pageWidth / 2 (even can be 0), as you just scroll up/down, and as a result, the page turn out to be 1.