iOS UIPageControl的简单使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    UIPageControl *pagelCtrl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 100, 320, 40)];
    pagelCtrl.backgroundColor=[UIColor grayColor];
    //指示器颜色
    pagelCtrl.pageIndicatorTintColor=[UIColor redColor];
    //    pagelCtrl.enabled=NO;
    //添加方法
    [pagelCtrl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    //耗内存...
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeValue:) userInfo:pagelCtrl repeats:YES];
    //当单页时隐藏
    pagelCtrl.hidesForSinglePage=YES;
    //页数
    pagelCtrl.numberOfPages=5;
    //当前页
    pagelCtrl.currentPage=3;
    //添加到window
    [self.window addSubview:pagelCtrl];
    [pagelCtrl release];//释放
    
    [self.window makeKeyAndVisible];
    return YES;
}
//pagelCtrl 响应的方法
-(void)pageChange:(UIPageControl *)pageContrl
{
    NSLog(@"当前的页数:%i",pageContrl.currentPage+1);
}

-(void)changeValue:(NSTimer *)timer
{
    UIPageControl *pagelCtrl=[timer userInfo];
    if (pagelCtrl.currentPage<4) {
        pagelCtrl.currentPage++;
    }
    else
    {
        pagelCtrl.currentPage=0;
    }
}

你可能感兴趣的:(iOS UIPageControl的简单使用)