ScrollView的基本使用

作者: 农民工小徐 | 来源:发表于2016-06-22 18:01 被阅读127次

基本使用步骤


  1. 创建一个scrollView,进行frame等常规设置,并将其添加到控制器view
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:scrollView];`

  2. 将需要展示的内容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];

  3. 设置scrollView的contentSize,一般比frame范围大,使scrollView可以滚动
    scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);

  4. 你还可以设置scrollView的其他属性
    scrollView.showsHorizontalScrollIndicator = NO; scrollView.showsVerticalScrollIndicator = NO; scrollView.contentInset = UIEdgeInsetsMake(50, 50, 50, 50); NSLog(@"%f",scrollView.contentOffset.x);

缩放手势的步骤


  1. 设置控制器为scrollView的代理
    @interface ViewController () <UIScrollViewDelegate> scrollView.delegate = self;
  2. 设置缩放的范围(最大比例和最小比例)
    scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = 0.5;
  3. 实现scrollView的代理方法,来确定scrollView内需要缩放的控件
    -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.imageView; }

相关文章

网友评论

    本文标题:ScrollView的基本使用

    本文链接:https://www.haomeiwen.com/subject/skrndttx.html