知識點: 1.UIScrollView使用 2.UIPageControl使用 @interface RootViewController ()<UIScrollViewDelegate> UIScrollView使用 1.UIScrollView創建 2.常用屬性 1)滾動內容大小 2)是否分頁滾 ...
知識點:
1.UIScrollView使用
2.UIPageControl使用
@interface RootViewController ()<UIScrollViewDelegate>
=======================
UIScrollView使用
1.UIScrollView創建
2.常用屬性
1)滾動內容大小
@property(nonatomic) CGSize contentSize //實例化一個滾動視圖 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; //設置滾動區域的大小 scrollView.contentSize = CGSizeMake(KScreenWidth * 3, KScreenHeigth *3);
2)是否分頁滾動
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled
scrollView.pagingEnabled = YES;
3)設置內容位置
@property(nonatomic) CGPoint contentOffset //屏幕的寬度 #define KScreenWidth [UIScreen mainScreen].bounds.size.width //屏幕的高度 #define KScreenHeigth [UIScreen mainScreen].bounds.size.height //設置偏移量 //scrollView.contentOffset = CGPointMake(KScreenWidth, KScreenHeigth); //帶動畫設置偏移量 [scrollView setContentOffset:CGPointMake(KScreenWidth, KScreenHeigth) animated:YES];
4)顯示水平滾動條
@property(nonatomic) BOOL showsHorizontalScrollIndicator
scrollView.showsHorizontalScrollIndicator= NO;
5)顯示垂直滾動條
@property(nonatomic) BOOL showsVerticalScrollIndicator
scrollView.showsVerticalScrollIndicator = NO;
6)彈簧效果
@property(nonatomic) BOOL bounces //關閉彈簧效果 scrollView.bounces = NO;
=======================
UIScrollView的常用代理方法
1)即將開始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
2)即將停止拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity targetContentOffset:
(inout CGPoint *)targetContentOffset
3)已經停止拖拽
scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
4)即將停止減速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
5)已經停止減速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
6)點擊狀態欄回到頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
7)已經滑到頂部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
8)放大縮小
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
9)縮放完畢
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view atScale:(CGFloat)scale
#pragma mark- UIScrollViewDelegate //準備開始拖拽 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"scrollViewWillBeginDragging"); } //準備停止拖拽 -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ NSLog(@"scrollViewWillEndDragging"); } //已經停止拖拽 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"scrollViewDidEndDragging"); } //準備開始減速 -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ NSLog(@"scrollViewWillBeginDecelerating"); } //已經停止減速 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"偏移量為%@",NSStringFromCGPoint(scrollView.contentOffset)); NSLog(@"scrollViewDidEndDecelerating"); } //滾動過程中一直調用的代理方法 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ // NSLog(@"偏移量為%@",NSStringFromCGPoint(scrollView.contentOffset)); // NSLog(@"scrollViewDidScroll"); } //允許點擊狀態欄滑動到頂部 -(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{ return YES; } //已經滑動到頂部 -(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{ //NSLog(@"scrollViewDidScrollToTop"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:@"已經滑到最頂" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; //展示 [alert show]; } //設置代理 _scrollView.delegate = self; //設置放大縮小的範圍 _scrollView.minimumZoomScale = 0.5; _scrollView.maximumZoomScale = 2; [self.view addSubview:_scrollView]; #pragma mark- UIScrollViewDelegate //返回需要放大的視圖 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return _imageView; } //放大縮小完成之後的回調方法 -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{ //view -> 當前放大縮小的視圖對象 //scale -> 放大縮小的繫數 NSLog(@"scale = %f",scale); //判斷 if (scale < 1.0) { //縮小 view.center = CGPointMake(scrollView.frame.size.width/2.0, scrollView.frame.size.height/2.0); }else{ //放大 view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height); } }
=======================
UIPageControl
1.UIPageControl創建方式
2.常用屬性
1)總頁數 @property(nonatomic) NSInteger numberOfPages //設置總頁數 _pageCtl.numberOfPages = 4; @property(nonatomic,retain) UIColor *pageIndicatorTintColor //設置當前頁碼顏色 _pageCtl.currentPageIndicatorTintColor = [UIColor greenColor]; //其他頁碼顏色 _pageCtl.pageIndicatorTintColor = [UIColor redColor]; 2)當前頁數 @property(nonatomic) NSInteger currentPage @property(nonatomic,retain) UIColor *currentPageIndicatorTintColor
3.結合UIScrollView
1)利用代理協議完成UIPageControl當前頁的設置
#pragma mark- UIScrollViewDelegate //停止減速 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (scrollView.contentOffset.x == self.view.frame.size.width * 5) { //不能用帶動畫跳轉 //當移動到第六個視圖的時候 //瞬間跳轉到第二個視圖 scrollView.contentOffset = CGPointMake(self.view.frame.size.width , 0); }else if (scrollView.contentOffset.x == 0){ //當移動到第一個視圖的時候 //瞬間跳轉到第五個視圖 scrollView.contentOffset = CGPointMake(self.view.frame.size.width * 4 , 0); } //設置頁碼 _pageCtl.currentPage = scrollView.contentOffset.x/self.view.frame.size.width - 1; }