基本使用步骤
创建一个scrollView,进行frame等常规设置,并将其添加到控制器view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:scrollView];`将需要展示的内容addSubView到scrollView中
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cao"]]; imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height); [scrollView addSubview:imageView];
设置scrollView的contentSize,一般比frame范围大,使scrollView可以滚动
scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
你还可以设置scrollView的其他属性
scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.contentInset = UIEdgeInsetsMake(50, 50, 50, 50); NSLog(@"%f",scrollView.contentOffset.x);
缩放手势的步骤
- 设置控制器为scrollView的代理
@interface ViewController () <UIScrollViewDelegate> scrollView.delegate = self;
- 设置缩放的范围(最大比例和最小比例)
scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = 0.5;
- 实现scrollView的代理方法,来确定scrollView内需要缩放的控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.imageView; }