- (void)viewDidLoad数组
{ide
CGRect rec = [[UIScreen mainScreen] bounds];spa
mainScrollView= [[UIScrollView alloc] initWithFrame:rec];orm
//设置实际内容区域大小(当前为水平滚动,高度设置为0是禁止垂直方向滚动)图片
picsScrollView.contentSize = CGSizeMake(rect.size.width * 4, 0);string
picsScrollView.backgroundColor = [UIColor blackColor];it
//当值是 YES 会自动滚动到 subview 的边界。默认是NO(至关于启用分页功能,滚动视图为页面翻转效果)io
picsScrollView.pagingEnabled = YES;变量
//滚动时是否显示水平滚动条,默认为YESobject
picsScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.delegate = self;
[self.view addSubview:mainScrollView];
//定义图片数组
self.imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.jpg"], nil];
[self settingScrollView];
//分页控件
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 0)];
pageControl.numberOfPages = [self.imageArray count];
// pageControl.pageIndicatorTintColor = [UIColor redColor];
// pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
[self scrollViewDidScroll:mainScrollView];
[self.view addSubview:pageControl];
self.navigationController.navigationBarHidden = NO;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark-自定义方法
-(void)settingScrollView
{
for (int i=0; i<3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[self.imageArray objectAtIndex:i]];
imageView.frame = CGRectMake(i*mainScrollView.frame.size.width, 0, mainScrollView.frame.size.width, mainScrollView.frame.size.height);
[mainScrollView addSubview:imageView];
}
}
#pragma mark - scollerView协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// self.navigationItem.title = [NSString stringWithFormat:@"(%.2f,%.2f)",scrollView.contentOffset.x,scrollView.contentOffset.y];
NSInteger pageInt = scrollView.contentOffset.x / scrollView.frame.size.width;
self.navigationItem.title = [NSString stringWithFormat:@"第%d张",pageInt+1];
pageControl.currentPage = pageInt;
pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
}
期间
1,由于定义的UIScrollerView的实例变量为scrollView和委托方法中传入的参数重名,致使出现了下面的警告:Local declaration of 'scrollView' hides instance variable
2,由于粗心误认为UIScrollerView不须要实现协议,
3,scrollView.contentOffset指的是每一张内容显示界面左上角的坐标,
4,UIPageControll和UIScrollerView之间没有什么联系,