iOS开发-UI (十)UIScrollView 和 UIPageControl使用
知识点:
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; }