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];
Related
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am making an iOS app and I need to show some kind of a horizontal scroll view with some table views in it. It must look this way:
https://dl.dropboxusercontent.com/u/53942814/Pic1.png
https://dl.dropboxusercontent.com/u/53942814/Pic2.png
When user swipes left or right the corresponding table view is to be displayed.
I found two ways to do it:
a UIScrollView with UITableViews
a UICollectionView.
Does anyone have in idea what is the best way and how to implement it?
I have implemented exact same in my code which replicates android like control…
Please see some part of my code you will get some ideas
I have done it by adding Views to UIScrollView.
Used 2 scrollviews one for title and one for tables
1. Adding table views to scrollview for table
pageNo = 1;
for (Page *page in pageArray)
{
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
PageContentViewController *contentViewController = [mystoryboard instantiateViewControllerWithIdentifier:#"pageContentViewController"];
contentViewController.detailsDictionary = page.deviceDetailDictionary; //assigning dictionary will use as table data source
/* Content page's x position calculation */
// Formula: xPos = scrollViewWidth * ( pageNo -1 )
// Where,
// pageNo starts with 1
int xPos = self.detailScrollView.frame.size.width * (pageNo -1);
contentViewController.view.frame = CGRectMake(xPos, 0, self.detailScrollView.frame.size.width, contentViewController.view.frame.size.height);
pageNo ++;
[self addChildViewController:contentViewController];
[self.detailScrollView addSubview:contentViewController.view];
[contentViewController didMoveToParentViewController:self];
}
2. Add UILabels to titleScrollView
Just check this formula to add title UILabel views to top scroll view
// Formula: xPos = 75 + 10 * pageNo + (pageNo -1) * 150;
// Where,
// pageNo starts with 1
// 75 = Left margin of first title
// 10 = Space between two labels
// 150 = Width of label
3. Use Delegate method of ScrollView (in which you added table) to handle swipes
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
CGFloat pageWidth = self.detailScrollView.frame.size.width;
//1. Calculate page no. if 50 percent or more area of any page visible make it as current page
int pageNo = floor((self.detailScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 2;
//2. Make only current page (calculated in 1.) visible by scrolling to that page in detailScrollView
//Formula : xPos = pageWidth * (pageNo -1)
CGRect frame = CGRectMake( pageWidth * (pageNo -1), 0, pageWidth, 50);
[self.detailScrollView scrollRectToVisible:frame animated:YES];//OR set content offset
//3. Make title visible of current page by scrolling to that title in titleScrollView
//Formula : xPos = (pageWidth / 2) * (pageNo -1)
CGRect titleFrame = CGRectMake( 160 * (pageNo -1), 0, pageWidth, 10);
[self.titleScrollView scrollRectToVisible:titleFrame animated:YES];//OR set content offset
//Fade effect for non current titles
//Set all page title alpha to low value
for (UILabel *title in pageTitleArray)
{
[title setAlpha:0.5];
}
//set current page alpha to 1
UILabel *title = [pageTitleArray objectAtIndex:pageNo -1];
[title setAlpha:1];
}
Code in 3 takes care of moving to next / prev page on swipe with animation
Hope this will be good starting point for you...
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 would like to know if it is possible for me to scroll a UIScrollView on the click of a UIButton and if so, would someone be able to tell me how to.
currently I am using the below code to see if in the scrollview more content is there to its left and if it is there, display an image which would tell the users that there is more content if they scroll to the left.
I would like to implement a functionality where I add a UIButton instead of the image and when more content is available on the left and when the user clicks the button, the scrollview would scroll to its left.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1 {
if (scrollView1.contentOffset.x == scrollView1.contentSize.width - scrollView1.frame.size.width)
{
// reached the right
self.imageView.hidden = YES;
}
else
{
self.imageView.hidden = NO;
}
}
It would be great if someone could help me out on this.
That's pretty easy, I think. Use:
[scrollView1 setContentOffset:CGPointMake(x, 0) animated:YES];
Where X is the value of scrollView1.contentOffset.x + scrollView1.contentSize.width.
for Objective C use:
-(IBAction)adjustScroll:(UIButton *)sender{
CGFloat xOffset = scrollView1.contentOffset.x;
CGFloat yOffset = scrollView1.contentOffset.y;
[scrollView1 setContentOffset:CGPointMake(xOffset + scrollView1.contentSize.width, yOffset) animated:YES];
}
for swift 4.2 use:
#IBAction func adjustScroll(sender:UIButton){
let xOffset = scrollView1.contentOffset.x
let yOffset = scrollView1.contentOffset.y
let customOffset = CGPoint(x: xOffset + scrollView1.contentSize.width, y: yOffset)
scrollView1.setContentOffset(customOffset, animated: true)
}