//
// ViewController.m
// UIPageControl 01
//
// Created by cqy on 16/2/15.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()<UIScrollViewDelegate>
{ UIScrollView *scroll;
UIPageControl *page;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
imgview.frame = CGRectMake(0, 0, 200, 200);
UIImageView *imgview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]];
imgview1.frame = CGRectMake(WIDTH, 0, 200, 200);
UIImageView *imgview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3"]];
imgview2.frame = CGRectMake(2*WIDTH, 00, 200, 200);
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,30, WIDTH, 200)];
[scroll addSubview:imgview];
[scroll addSubview:imgview1];
[scroll addSubview:imgview2];
scroll.backgroundColor = [UIColor greenColor];
scroll.contentSize = CGSizeMake(2*WIDTH, 200);
scroll.delegate = self;
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
[self.view addSubview:scroll];
page = [[UIPageControl alloc] initWithFrame:CGRectMake(70, 350, 200, 40)];
page.backgroundColor = [UIColor whiteColor];
page.numberOfPages = 3;//设置页数(多少个点)
page.currentPage = 2;//设置当前选中页
NSLog(@"--%zi--",page.currentPage);//获取当前选中页下标
page.pageIndicatorTintColor = [UIColor grayColor];//未选中颜色
page.currentPageIndicatorTintColor = [UIColor greenColor];//选中颜色
[page addTarget:self action:@selector(trigger:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:page];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)trigger: (UIPageControl *) tri{
NSLog(@"-%zi-",[page currentPage]);
CGPoint p = {[page currentPage]*300,0};
[scroll setContentOffset:p animated:YES];
NSLog(@"trigger...");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UIPageControl
原文:http://www.cnblogs.com/iQingYang/p/5193216.html