2013年4月1日 星期一
循環uiscroll﹠page view
1,在viewcontroller先宣告
2,在viewdidload
//新增自己的View
MyView=self.advertisementView;
//MyView = [[[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, 180.f)]autorelease];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10.f, 10.f, self.view.frame.size.width-20.f, 150.f) ];
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0.f, 160.f, self.view.frame.size.width, 15.f)];
//
_colors = [[NSArray alloc]initWithObjects:
[UIColor redColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor yellowColor],
[UIColor orangeColor],
nil];
self.pagesArray = [[NSMutableArray alloc] initWithArray:_colors];
//ScrollView 的內容總尺吋 : 原寬 x 有幾個分頁, 高不變
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height);
//設定是否啟動分頁機制 : 如不啟動,則會一直滑動不停 ; 如啟動,會一格一格的分頁顯示
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
//設定 PageControl 的樣式
self.pageControl.backgroundColor = [UIColor clearColor];
self.pageControl.numberOfPages = self.pagesArray.count;
self.pageControl.currentPage = 0;
//設定 PageControl 的分頁數
self.pageControl.numberOfPages = self.pagesArray.count;
self.pageControl.currentPage = 0;
MyView.backgroundColor = [UIColor clearColor];
CGRect frame1;
//定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 )
frame1.origin.x = self.scrollView.frame.size.width * 0;
// Y 軸不變
frame1.origin.y = 0;
frame1.size = self.scrollView.frame.size;
subview1 = [[UIView alloc] initWithFrame:frame1];
subview1.backgroundColor = [self.colors objectAtIndex:4];
CGRect frame2;
//定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 )
frame2.origin.x = self.scrollView.frame.size.width * 1;
// Y 軸不變
frame2.origin.y = 0;
frame2.size = self.scrollView.frame.size;
subview2 = [[UIView alloc] initWithFrame:frame2];
subview2.backgroundColor = [self.colors objectAtIndex:0];
CGRect frame3;
//定位每一個分頁 View 在 ScrollView 裡的位置( X 軸 )
frame3.origin.x = self.scrollView.frame.size.width * 2;
// Y 軸不變
frame3.origin.y = 0;
frame3.size = self.scrollView.frame.size;
subview3 = [[UIView alloc] initWithFrame:frame3];
subview3.backgroundColor = [self.colors objectAtIndex:1];
//使用QuartzCore.framework替UIView加上圓角
//[subview1.layer setCornerRadius:15.0];
[_scrollView addSubview:subview1 ];
[_scrollView addSubview:subview2 ];
[_scrollView addSubview:subview3 ];
[self.scrollView scrollRectToVisible:CGRectMake(300.f, 0.f, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
[MyView addSubview:self.scrollView];
[MyView addSubview:self.pageControl];
3,實作uiscrollviewdelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"%d",self.pageControl.numberOfPages);
if (self.scrollView.contentOffset.x == 0.f) {
if (self.pageControl.currentPage==0) {
self.pageControl.currentPage = self.pageControl.numberOfPages-1;
}else{
self.pageControl.currentPage --;
}
}else if(self.scrollView.contentOffset.x == 600.f){
if (self.pageControl.currentPage==self.pageControl.numberOfPages-1) {
self.pageControl.currentPage = 0;
}else{
self.pageControl.currentPage++;
}
}
int lastPage,currentPage,nextPage;
currentPage = self.pageControl.currentPage;
if (self.pageControl.currentPage==self.pageControl.numberOfPages-1) {
lastPage = currentPage -1;
nextPage = 0;
}else if(self.pageControl.currentPage==0){
lastPage = self.pageControl.numberOfPages-1;
nextPage = currentPage +1;
}else{
lastPage = currentPage -1;
nextPage = currentPage +1;
}
NSLog(@"next %d",nextPage);
NSLog(@"current %d",currentPage);
NSLog(@"last %d",lastPage);
subview1.backgroundColor = [_colors objectAtIndex:lastPage];
subview2.backgroundColor = [_colors objectAtIndex:currentPage];
subview3.backgroundColor = [_colors objectAtIndex:nextPage];
NSLog(@"%d",self.pageControl.currentPage);
[self.scrollView scrollRectToVisible:CGRectMake(300.f, 0.f, self.scrollView.frame.size.width, self.scrollView.frame.size.height) animated:NO];
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言