初始化及添加背景色
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
scrollView.backgroundColor = [UIColor cyanColor];
設置顯示內容區域
scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 6);
關閉滾動條
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
整頁滑動
scrollView.pagingEnabled = YES;
設置回彈效果(單獨設置沒有效果,三個一起設置)
scrollView.bounces = YES;
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;
設置縮放比例
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2
設置代理
scrollView.delegate = self;
顯示視圖及釋放內存
[self.view addSubview:scrollView]
[scrollView release]
添加圖片
UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageV.image = [UIImage imageNamed:@"1.jpg"];
imageV.tag = 100;
[scrollView addSubView:imageV];
[imageV release];
添加還原縮放的按鈕
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor greenColor];
[button addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
- (void)click:(UIButton *)button{
UIScrollView *scroll = (UIScrollView *)[self.view viewWithTag:101];
[scroll setZoomScale:1 animated:YES];
[scroll setContentOffset:CGPointMake(0, kScreenHeight)animated: YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
imageView.center = self.view.center;
NSLog(@"縮放時 一直觸發");
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return scrollView.subviews[0];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"將要開始拖動");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"已經結束拖拽");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"將要開始減速");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"已經結束減速");
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
NSLog(@"縮放已經結束觸發");
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
NSLog(@"改變偏移量的動畫結束後觸發");
}
UIPageControl
初始化
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50)];
pageControl.backgroundColor = [UIColor blackColor];
設置共多少頁
pageControl.numberOfPages = 5;
設置當前顯示第幾頁
pageControl.currentPage = 2;
設置選中和未選中的顏色
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
添加單擊響應事件
[pageControl addTarget:self action:@selector(page:) forControlEvents:(UIControlEventTouchUpInside)];
添加顯示視圖
[self.view addSubview:pageControl];
[pageControl release];
實現單機響應事件方法
- (void)page:(UIPageControl *)page{
NSLog(@"%ld",page.currentPage);
}