刚刚遛狗回来,前段时间创建的这篇博客一直没有填充内容,今天把scrollerview正好整理一下。
1.scrollerview的主要作用:当界面显示不开要显示的内容,scrollerview提供了滑动的效果,使用户能够通过滑动查看所有的内容
2.scrollerview的常用属性:
(1)CGSizecontentSize;这个属性⽤用来表⽰示UIScrollView内容的尺寸,滚动范围
(2)CGPointcontentOffset; 这个属性⽤来表示UIScrollView滚动的位置,表示的是一个点:CGPoint,也就是scrollerview的“显示界面”的原点位置在“内容界面”的位置。比如scrollerview的frame的是 CGRectMake(0,0,320,320) ,而scrollerview.contantSize = CGSizeMake(320,640); ,那么当内容滑动到底部时候,CGPointcontentOffset就是(0,320)。
(3)UIEdgeInsetscontentInset; 这个属性能够在UIScrollView的4周增加额外的滚动区域,(0,0,0,0)表示 上 左 下 右,即逆时针
(4)@property(nonatomic) BOOL bounces; 设置UIScrollView是否需要弹簧效果
(5)@property(nonatomic,getter=isScrollEnabled)BOOLscrollEnabled; 设置UIScrollView是否能滚动
(6)@property(nonatomic) BOOL showsHorizontalScrollIndicator; 是否显⽰示⽔水平滚动条
(7)@property(nonatomic) BOOL showsVerticalScrollIndicator; 是否显⽰示垂直滚动条
3.注意点
• 如果UIScrollView⽆无法滚动,可能是以下原因:
(1)没有设置contentSize
(2) scrollEnabled = NO
(3) 没有接收到触摸事件:userInteractionEnabled = NO
(4)没有取消autolayout功能(要想scrollView滚动,必须取消autolayout)
4.代码
(1)创建scrollerview
- (void)viewDidLoad { [super viewDidLoad]; // 1.创建UIScrollView UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(0, 0, 250, 250); // frame中的size指UIScrollView的可视范围 scrollView.backgroundColor = [UIColor grayColor]; [self.view addSubview:scrollView]; // 2.创建UIImageView(图片) UIImageView *imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:@"big.jpg"]; CGFloat imgW = imageView.image.size.width; // 图片的宽度 CGFloat imgH = imageView.image.size.height; // 图片的高度 imageView.frame = CGRectMake(0, 0, imgW, imgH); [scrollView addSubview:imageView]; // 3.设置scrollView的属性 // 设置UIScrollView的滚动范围(内容大小) scrollView.contentSize = imageView.image.size; // 隐藏水平滚动条 scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; // 用来记录scrollview滚动的位置 // scrollView.contentOffset = ; // 去掉弹簧效果 // scrollView.bounces = NO; // 增加额外的滚动区域(逆时针,上、左、下、右) // top left bottom right scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20); _scrollView = scrollView; } - (IBAction)down:(UIButton *)sender { [UIView animateWithDuration:1.0 animations:^{ //三个步骤 CGPoint offset = _scrollView.contentOffset; offset.y += 150; _scrollView.contentOffset = offset; //_scrollView.contentOffset = CGPointMake(0, 0); }]; } @end
(2)缩放功能
#import "YYViewController.h" @interface YYViewController () <UIScrollViewDelegate> { UIScrollView *_scrollview; UIImageView *_imageview; } @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; //1添加 UIScrollView //设置 UIScrollView的位置与屏幕大小相同 _scrollview=[[UIScrollView alloc]initWithFrame:self.view.bounds]; [self.view addSubview:_scrollview]; //2添加图片 //有两种方式 //(1)一般方式 // UIImageView *imageview=[[UIImageView alloc]init]; // UIImage *image=[UIImage imageNamed:@"minion"]; // imageview.image=image; // imageview.frame=CGRectMake(0, 0, image.size.width, image.size.height); //(2)使用构造方法 UIImage *image=[UIImage imageNamed:@"minion"]; _imageview=[[UIImageView alloc]initWithImage:image]; //调用initWithImage:方法,它创建出来的imageview的宽高和图片的宽高一样 [_scrollview addSubview:_imageview]; //设置UIScrollView的滚动范围和图片的真实尺寸一致 _scrollview.contentSize=image.size; //设置实现缩放 //设置代理scrollview的代理对象 _scrollview.delegate=self; //设置最大伸缩比例 _scrollview.maximumZoomScale=2.0; //设置最小伸缩比例 _scrollview.minimumZoomScale=0.5; } //告诉scrollview要缩放的是哪个子控件 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageview; } @end
本文参考文顶顶的博客:http://www.cnblogs.com/wendingding/p/3754268.html