Kindle iPhone App Navigation - how to - iphone

I like the kindle for iPhone's navigation - the one which enables you to just slide the titles in the navigation bar to move from one view to another. (I'm attaching the image)
However, I was searching for a long time yesterday and I cannot find anybody who has done something similar.
Would you have any ideas on how to do this?
UPDATE: May 24, 2012
So now I have codes. Thanks to this tutorial http://bit.ly/xNOiqd. (I just copied most of his code.) But I still cannot achieve the same effect that the kindle app's nav bar has. I can now peek on the left and the right item but my scroll view skips the labels which are on the odd indices of my array. Let's say I have 'Favourites', 'All Products', and 'Categories'. When I swipe left, I move from 'Favourites' to 'Categories' and skip 'All Products'
Here's the code.
#pragma mark -
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages we want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.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.origin.x = frame.size.width * page;
frame.origin.x = (frame.size.width /2) * page;
frame.origin.y = 0.0f;
//
frame = CGRectInset(frame, 10.0f, 0.0f);
//self.scrollView.bounds = frame;
/* == orig line
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
*/
UILabel *newPageView = [[UILabel alloc] init];
newPageView.text = [self.pageImages objectAtIndex:page];
newPageView.textColor = [UIColor whiteColor];
//newPageView.textColor = [UIColor grayColor];
newPageView.textAlignment = UITextAlignmentCenter;
newPageView.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
[newPageView setBackgroundColor:[UIColor clearColor]];
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
//[pageView removeFromSuperview];
//[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//scroll view stuff
self.scrollView.delegate = self;
//attempt to make size of scrollview smaller
//CGRect frame = self.scrollView.bounds;
//frame = CGRectInset(frame, 10.0f, 0.0f);
//CGFloat width = self.scrollView.frame.size.width/2.0f;
self.pageImages = [NSArray arrayWithObjects:#"Favourites", #"All Products", #"Categories",#"Boo yah", nil];
NSInteger pageCount = self.pageImages.count;
// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// Set up the array to hold the views for each page
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
CGSize pagesScrollViewSize = self.scrollView.frame.size;
//self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
//This modifies the size of the scroll view, I can't seem to get it right
self.scrollView.contentSize = CGSizeMake((pagesScrollViewSize.width / 1.50f )* self.pageImages.count, self.scrollView.frame.size.height);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages which are now on screen
[self loadVisiblePages];
}

iCarousel is a nice open source project with lots of scroll types.

A paged scrollview and AQGridView most likely. If not, it would be an acceptable reverse-engineering attempt anyways.

Related

How do I change the current page for Ray Wenderlich's UIScrollView Tutorial?

I am using Ray Wenderlich's (Matt Galloway's) tutorial on UIScrollViews:
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
I am using the section, "Paging with UIScrollView" in my app. This works great when you always want the page to load on 0, but I would like the current page to be dynamic. I will be passing this information in based on which thumbnail the user picked from the previous view controller, which is an NSInteger called "listIndex."
How do I change the current page to my listIndex? I have tried changing
self.pageControl.currentPage = 0;
to
self.pageControl.currentPage = self.listIndex;
in viewDidLoad, but this doesn't seem to help - possibly because it's getting reset in loadVisiblePages?
Thank you so much for the help! Here's the code:
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *myHTML = [[NSMutableString alloc] initWithString:#"<html><head><style type='text/css'> p {font-family:sans-serif; text-shadow: 3px 4px 5px #000; color:#FFF; position: absolute; bottom:0; font-size:10pt; font-weight:bold; margin-bottom:5px;}</style><body><p>"];
[myHTML appendString:self.caption];
[myHTML appendString:#"</p></body></html>"];
[captionWebView loadHTMLString:myHTML baseURL:nil];
self.pageImages = [NSMutableArray array];
for (int i=0; i < [imageList count]; i++) {
NSString *path = [(Image *)[imageList objectAtIndex:i] path];
[self.pageImages addObject:[UIImage imageNamed:path]];
}
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
//self.pageControl.currentPage = self.listIndex; //Tried this
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.frame = CGRectMake(0, 0, 320, 468);
} else {
self.scrollView.frame = CGRectMake(0, 0, 320, 380);
}
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
- (void)viewWillDisappear:(BOOL)animated {
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.contentSize = CGSizeMake(320, 468);
} else {
self.scrollView.contentSize = CGSizeMake(320, 380);
}
}
Try setting the scrollview's initial contentOffset in viewWillAppear::
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// Set initial content offset of scrollview
self.scrollView.contentOffset = CGPointMake(pagesScrollViewSize.width * self.listIndex, self.scrollView.contentOffset.y);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}

PageControl Issue in iphone

I have a gridview when i click on any image i send current index of that gridview so i can see that image. and I am loading images from doucment.
below is my viewDidload code.
but what my problem is. when i click any gridview item . let say its index is 4. and i set it here in currentpage=4. so when i open this page. it shows me 4th image. but my page controller current page is 4 but i can see my image which was at index 1.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
UIImageView *subview;
//NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor orangeColor],[UIColor blueColor], nil];
for (int i = 0; i < (NSInteger)totalImagesInGrid; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
//subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[tableDataArray objectAtIndex:i]]];
NSString *name = [tableDataArray objectAtIndex:(NSUInteger)i];
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:name];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
subview.image = loadedImage;
[self.scrollView addSubview:subview];
[subview release];
}
//float count = [totalImagesInGrid floatValue];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 13, self.scrollView.frame.size.height);
self.pageControl.currentPage = (NSInteger)currentImage;
NSLog(#"currentPage %d",(NSInteger)currentImage);
self.pageControl.numberOfPages = (NSInteger)totalImagesInGrid;
NSLog(#"(NSInteger)totalImagesInGrid %d",(NSInteger)totalImagesInGrid);
}
- (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 = (NSInteger)currentImage + page;
NSLog(#"Page %d",page);
}
NSLog(#"scrollViewDidScroll");
}
- (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];
pageControlBeingUsed = YES;
}
this is my gridview and i m click on (1,1) image.
And this is my page control view. but thats not the image i have selected . this is (0,1) image but see at page control it is on index 2.
set page index with selectedIndex of gridview for example see bellow...
- (void) gridView:(UIGridView *)grid didSelectRowAt:(int)rowIndex AndColumnAt:(int)colIndex{
///set Image with index of colIndex..
int selectedIndex = (rowIndex*4)+colIndex;
NSLog(#"\n Selected Image Index is %d",selectedIndex);
}
and in SecondView Class's viewDidLoad: just call your secondView class method changePage at last of the method like bellow
- (void)viewDidLoad {
////another your code
[self changePage]
}
i hope this help you..

vertical images in UIScrollView

I tried to create a list of UIimages in a UIScrollview. When I implement it horizontally it works the way I want like:
As you see the images stick together.
Then I tried it vertically. it works but an unwanted space between the images happens like:
This is my code:
#synthesize scrollView, pageControl;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pageControlBeingUsed = NO;
NSArray *pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"photo1.png"],
[UIImage imageNamed:#"photo2.png"],
[UIImage imageNamed:#"photo3.png"],
[UIImage imageNamed:#"photo4.png"],
[UIImage imageNamed:#"photo5.png"],
nil];
for (int i = 0; i < pageImages.count; i++) {
CGRect frame;
//frame.origin.x = self.scrollView.frame.size.width * i;
//frame.origin.y = 0;
frame.origin.x = 0;
frame.origin.y = self.scrollView.frame.size.height * i;
frame.size = self.scrollView.frame.size;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
}
//self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * pageImages.count , self.scrollView.frame.size.height);
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width , self.scrollView.frame.size.height* pageImages.count);
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageImages.count;
}
- (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.height;
int page = floor((self.scrollView.contentOffset.y - pageWidth / 2) / pageWidth) + 1;
//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 = 0;
frame.origin.y = self.scrollView.frame.size.height * self.pageControl.currentPage;
// 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;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.scrollView = nil;
self.pageControl = nil;
}
- (void)dealloc {
[scrollView release];
[pageControl release];
[super dealloc];
}
#end
in the code the horizontal code lines are commented.
Can you help me to remove this unwanted space in vertical one?
Here the image size makes the problem.
Check with this newPageView.contentMode = UIViewContentModeScaleAspectFill; instead of newPageView.contentMode = UIViewContentModeScaleAspectFit;
edit this part of your code
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[pageImages objectAtIndex:i]];
newPageView.contentMode = UIViewContentModeScaleAspectFit; // this statement is making your newPageView bigger then the image, hence spaces are coming when scrolling. !!
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
dynamically create your view "newPageView" such that the view is of exact size with your images.

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;
}

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);