iOS_7_scrollView大图缩放
阅读原文时间:2023年07月09日阅读:1

终于效果图:

BeyondViewController.h

//
// BeyondViewController.h
// 7_scrollView大图展示
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import

@interface BeyondViewController : UIViewController

  • (IBAction)upBtnClick:(id)sender;

@end

BeyondViewController.m

//
// BeyondViewController.m
// 7_scrollView大图展示
//
// Created by beyond on 14-7-24.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "BeyondViewController.h"

@interface BeyondViewController ()
{
UIScrollView *_scrollView;

// 要缩放的是内部的哪一个控件  
UIImageView \*\_imgView;  

}

@end

@implementation BeyondViewController

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // UIImageView
    // 1,类方法创建控件
    // UIImageView *imgView = [[UIImageView alloc]init];
    // 2,控件细节
    // NSString *imgName = [NSString stringWithFormat:@"HD.jpg"];
    // imgView.image = [UIImage imageNamed:imgName];
    // CGFloat imgW = imgView.image.size.width;
    // CGFloat imgH = imgView.image.size.height;
    // imgView.frame = CGRectMake(0, 0, imgW, imgH);

    // 或者高速创建,一句顶上面的6句代码
    _imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"HD.jpg"]];

    // 1,实例化UIScrollView
    _scrollView = [[UIScrollView alloc]init];
    // 2,设置scrollView的可视大小,内容大小,等属性
    // _scrollView.frame = CGRectMake(0, 0, 320, 480);
    _scrollView.frame = self.view.bounds;
    _scrollView.contentSize = _imgView.image.size; // 这个最重要,是滚动区域
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bouncesZoom = NO;
    _scrollView.bounces = YES;
    // scrollView.contentOffset是个点,x(左)和y(上),值是scrollView里面的大图片拖拽的位移,相对于scrollView的显示窗体的最左上角

    // 上左下右 逆时针 到边界之后,回不去了,多出来的距离
    // scrollView.contentInset = UIEdgeInsetsMake(5, 10, 20, 40);
    // 3,将imgeView加入到scrollView
    [_scrollView addSubview:_imgView];
    // 4,将scrollView加入到self.view
    // [self.view addSubview:_scrollView];
    // 小bug解决,界面上的button被全然遮挡了
    [self.view insertSubview:_scrollView atIndex:0];

    /*
    4.scrollView实现缩放(捏合手势)四步曲
    1,设置代理 为 当前控制器
    2,代理 尊守 协议
    3,代理 实现 协议中的方法 viewForZoomingInScrollView,告诉scrollView要缩放的是它内部的哪一个控件
    4,scrollView设置最大最小缩放比 就可以
    */
    _scrollView.delegate = self;
    _scrollView.minimumZoomScale = 0.3;
    _scrollView.maximumZoomScale = 2.0;

}

// 代理 实现 协议中的方法,告诉scrollView要缩放的是它内部的哪一个控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imgView;
}

  • (IBAction)upBtnClick:(id)sender {
    // scrollView.contentOffset是个点,x(左)和y(上),值是scrollView里面的大图片拖拽的位移,相对于scrollView的显示窗体的最左上角
    [UIView animateWithDuration:0.3 animations:^{
    // 下面三步为OC标准代码,由于OC中不同意直接修该对象中结构体属性的成员的值,要通过中间的暂时结构体变量
    CGPoint offset = _scrollView.contentOffset;
    if (offset.y + 80 >= _scrollView.contentSize.height - _scrollView.frame.size.height) {
    return;
    }
    offset.y += 80;
    _scrollView.contentOffset = offset;
    }];

}
@end

scrollView的contentOffset

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

scrollView原理是通过拖拽其内部的大图片

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章