pagecontrol help

Discussion in 'iOS Programming' started by cinek, Dec 2, 2011.

  1. cinek macrumors 6502

    Joined:
    Mar 12, 2011
    #1
    I'm trying to get pagecontrol to work. Right now I can scroll to the 2nd page, but the background is not changing (im changing the background to make sure it works fine). When I use pageControl to flip through pages, the background changes fine. I think I need to call pageTurning in scrollViewDidScroll but adding [self pageTurning:page]; does not work

    this is what I have atm:

    Code:
    static int kNumberOfPages = 2;
    
    #import "AppDelegate.h"
    
    @implementation AppDelegate
    
    @synthesize window = _window;
    @synthesize pageControl, scrollView, viewControllers;
    
    -(IBAction)changePage:(id)sender
    {
        pageControlUsed = YES;
    }
    
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        pageControlUsed = NO;
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)sender
    {
        if(pageControlUsed){
            return;
        }
        
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((scrollView.contentOffset.x - pageWidth / 2)/pageWidth) + 1;
        pageControl.currentPage = page;
        
    }
    
    -(void)pageTurning:(UIPageControl *)pageController{
        NSInteger nextPage = [pageController currentPage];
        
        switch (nextPage) {
            case 0:
                scrollView.backgroundColor = [UIColor blueColor];
                break;
            case 1:
                scrollView.backgroundColor = [UIColor redColor];
                
            default:
                break;
        }
    }
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
        scrollView.delegate = self;
        
        [pageControl addTarget:self action:@selector(pageTurning:) forControlEvents:UIControlEventValueChanged];
        
        pageControl.currentPage = 0;
        
        [self pageTurning:pageControl];
        
        //self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        //self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    any ideas?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Start with Apple's PageControl sample code. The code you show should be in a view controller, not the app delegate.
     
  3. cinek thread starter macrumors 6502

    Joined:
    Mar 12, 2011

Share This Page