设置标签
- (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]; } //定义PageControll self.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的滚动位置,以此切换到指定的页面