- (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之間沒有什麼聯繫,