iOS開發-UI (十)UIScrollView 和 UIPageControl使用,-uiuiscrollview
知識點:
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 pagingEnabledscrollView.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 showsHorizontalScrollIndicatorscrollView.showsHorizontalScrollIndicator= NO;
5)顯示垂直捲軸
@property(nonatomic) BOOL showsVerticalScrollIndicatorscrollView.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;}