Slide in UIScrollView similar to ios home screen - iphone

I have UIScrollView . I add images as subviews to it . The question is how to make slide similar as iOS home screen. Maybe it can be done with calculating end point of the scroll, and if it's x position is bigger than a half of scrollView width ,then do UIScrollView setContentOffset:animated: method. Any idea ?

delegate
#interface ViewController : UIViewController<UIScrollViewDelegate>
Scrollview:
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(960.0f, 480.0f); // customize it
and
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pagecontrol.currentPage = page;
}
PageControl value changed selector:
- (IBAction)changePage:(id)sender {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pagecontrol.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
more info: http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

Related

jump to pages in a scrollview that is using UIPagecontrol

I have UIScrollview that contains different UITableviews. You can scroll through these UITableviews using a UIPagecontrol.
This is how I add my UITableview to the UIScrollview
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageStages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
//frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
float offset=20;
frame.size.width = frame.size.width-(2*offset);
frame.origin.x = (326 * page)+offset;
frame.origin.y = 0;
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame];
tableStage.backgroundColor = [UIColor colorWithRed:(250/255.0) green:(248/255.0) blue:(247/255.0) alpha:100];
tableStage.contentMode = UIViewContentModeScaleAspectFit;
tableStage.delegate = self;
tableStage.dataSource = self;
tableStage.tag = page;
tableStage.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:tableStage];
[self.pageViews replaceObjectAtIndex:page withObject:tableStage];
}
}
Now I want that user can jump to a certain UITableview from another UIViewController. Therefore I have a variable pageNumber. When I set the pageNumber I call a method jumpToPage3 and I give the pageNumber with it. This is how the function looks like.
-(void)jumpToPage3:(NSNumber *)page{
NSLog(#"jump to page 3");
float offset=30;
float height = self.scrollView.bounds.size.height;
float width = 320 - (2*offset);
int pagee = [page intValue] - 1;
float x = (320 * pagee) + offset;
[self.scrollView scrollRectToVisible:CGRectMake(x, 0, width , height) animated:NO];
}
The problem is that it is not jumping to the correct page. It stays on the first page.
Can someone help me with this?
When you add content to the scroll view you also have to tell increase the contentSize

dynamic UIScrollView with paging and data from NSMutableArray

I saw alot of people asking about it, I read everything but still could not figure how to do it. I have a UIScrollView with paging enabled, also I have a NSMutableArray with strings.
what I want to do is that the UIScrollView will show to UITextView with the string and when I change to the next page, it will show the next item on the NSMutableArray.
for some reason I can't get it to work.
- (void)setupPage
{
pagingView.delegate = self;
[self.pagingView setBackgroundColor:[UIColor blackColor]];
[pagingView setCanCancelContentTouches:NO];
pagingView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
pagingView.clipsToBounds = YES;
pagingView.scrollEnabled = YES;
pagingView.pagingEnabled = YES;
pagingView.backgroundColor = [UIColor clearColor];
[pagingView setShowsHorizontalScrollIndicator:NO];
[pagingView setShowsVerticalScrollIndicator:NO];
for (int i=0;i<[arrCat count];i++)
{
UITextView * txtJoke = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
[pagingView addSubview:txtJoke];
txtJoke.text = [arrCat objectAtIndex:i];
txtJoke.textAlignment = UITextAlignmentRight;
}
[pagingView setContentSize:CGSizeMake(960, [pagingView bounds].size.height)];
}
- (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;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
- (IBAction)changePage:(id)sender :(int)current
{
/*
* Change the scroll view
*/
CGRect frame = pagingView.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
[pagingView scrollRectToVisible:frame animated:YES];
/*
* When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
This code is a combination of something from another app that i'm trying to fit into this, but without any success.
Please help. Thanks alot.
For one thing, all of your UITextView's have the same frame coordinates, so they will be on top of each other. Assuming you're scrolling horizontally, you need to offset the UITextView's x origin by the page width.
Are you using a UIPageControl? If not, you can just deleted the methods other than setupPage. Those other methods are just used for changing the UIPageControl, and changing the page using the UIPageControl.
Here is a basic scroll view implementation similar to yours:
static NSUInteger kNumberOfPages = 3;
static NSUInteger kPageWidth = 320;
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(kPageWidth * kNumberOfPages, self.scrollView.frame.size.height);
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.pageOne.frame = CGRectMake(0, 0, kPageWidth, self.pageOne.frame.size.height);
[self.scrollView addSubview:self.pageOne];
self.pageTwo.frame = CGRectMake(kPageWidth, 0, kPageWidth, self.pageTwo.frame.size.height);
[self.scrollView addSubview:self.pageTwo];
self.pageThree.frame = CGRectMake(2*kPageWidth, 0, kPageWidth, self.pageThree.frame.size.height);
[self.scrollView addSubview:self.pageThree];
self.pageControl.numberOfPages = kNumberOfPages;
self.pageControl.currentPage = 0;
}
# pragma mark - Scroll View Related Methods
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed_)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
int page = floor((self.scrollView.contentOffset.x - kPageWidth / 2) / kPageWidth) + 1;
self.pageControl.currentPage = page;
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
- (IBAction)changePage:(id)sender
{
int page = self.pageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed_ = YES;
}

Scroll view issue

friends
I develop one scroll view set its content size and set three view on scroll view and its scroll fine and i also want it move when segmentcontrol tap but it was not happen my view is not move on scroll can some one help me to figure out this problem.
hope i understand your question
try this
-(IBAction)SegmentControll:(id)sender
{
[myTableView removeFromSuperview];
if (SegmentControll.selectedSegmentIndex==0)
{
Yourscollview.scrollEnabled=NO;
}
if (SegmentControll.selectedSegmentIndex==1)
{
Yourscollview.scrollEnabled=YES;
}
}
if need more help ask me.
Move your scroll-view with tap on the Segment-control...Here, I am providing you sample code...you just need to decide on which segment index you want to move scroll view...
if (SegmentControll.selectedSegmentIndex==0)
{
if ( self.scrollView.contentOffset.x <= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
//pageControlBeingUsed = YES;
}
}
else if (SegmentControll.selectedSegmentIndex==1)
{
if ( self.scrollView.contentOffset.x >= self.scrollView.frame.size.width ) {
CGRect frame;
frame.origin.x = self.scrollView.contentOffset.x - self.scrollView.frame.size.width;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
//pageControlBeingUsed = YES;
}
}
I hope this will help you...

iPhone:UIScrollView with Paging in Landscape Mode Issue

I am build an app in Landscape mode only and I have an issue is that not fill up color with width in a Landscape mode in iPhone but I can't understand how can i do that so please give me some idea to develop this functionality.
Refer https://github.com/cwalcott/UIScrollView-Paging
Thanks in advance.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
pageControlBeingUsed = NO;
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = colors.count;
[super viewDidLoad];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (!pageControlBeingUsed)
{
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlBeingUsed = NO;
}
- (IBAction)changePage
{
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}
Solve my issue by just remove inner subView in Autosizing property of UIScrollView in interface Builder.
I think the reason will be
self.scrollView.frame.size returns (768,1024) in viewDidLoad: method but actually you need (1024,768). To solve this,
1) In IB, Attribute Inspecter change orientation to landscape mode.
or
2) Change
frame.size = self.scrollView.frame.size;
to
frame.size = CGSizeMake(1024,768);

uiscrollview into pages and move between the pages using an up and down button

I need to make my scrollview into pages and move between the pages using an up and down button.
How to achieve moving between pages with a button?
create a changePage method to call - same as you typically would with a pageControl.
I would suggest if you haven't done this before, try implementing with a pageControl and then switch to a button once you have that sorted.
-(IBAction)changePage:(id)sender {
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scroll scrollRectToVisible:frame animated:YES];
pageControlIsChangingPage = YES;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
pageControlIsChangingPage = NO;
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
-(void)scrollViewDidScroll:(UIScrollView *)_scrollView {
if (pageControlIsChangingPage) {
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}