- (void)viewDidLoad{[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//定义UIScrollView//self.scrollView = [[UIScrollView alloc] init];//self.scrollView.frame = CGRectMake(10, 0, 200, 500);self.scrollView.contentSize = CGSizeMake(1000,288); //scrollview的滚动范围self.scrollView.showsVerticalScrollIndicator = NO;self.scrollView.showsHorizontalScrollIndicator = NO;//myScrollView.clipsToBounds = YES;self.scrollView.delegate = self;self.scrollView.scrollEnabled = YES;self.scrollView.pagingEnabled = YES; //使用翻页属性self.scrollView.bounces = NO;for(int i=0;i<4;i++){UIImageView *imageV =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wel2.png"]];[imageV setFrame:CGRectMake(200*i, 0, 192, 288)];[self.scrollView addSubview:imageV];}//定义PageControllself.pageControl.numberOfPages = 4;//指定页面个数self.pageControl.currentPage = 0;//指定pagecontroll的值,默认选中的小白点(第一个)[self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];}//scrollview的委托方法,当滚动时执行- (void)scrollViewDidScroll:(UIScrollView *)sender {int page = self.scrollView.contentOffset.x / 192;//通过滚动的偏移量来判断目前页面所对应的小白点self.pageControl.currentPage = page;//pagecontroll响应值的变化}- (IBAction)changePage:(id)sender {int page = self.pageControl.currentPage;//获取当前pagecontroll的值self.label1.text = [[NSString alloc]initWithFormat:@"%d",page];[self.scrollView setContentOffset:CGPointMake(200 * page, 0)];//根据pagecontroll的值来改变scrollview的滚动位置,以此切换到指定的页面}
转载于:https://www.cnblogs.com/zzili/archive/2012/12/06/6663418.html