码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-UIScrollView-图片缩放

时间:2016-05-31 20:55:59      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

一. 实现功能

 两个手指捏合,可以放大或者缩小图片.

二.原理说明

1. 实现缩放功能的四个步骤

    (1) 让控制器遵守代理协议

    (2) 让scrollView设置代理 self

    (3) 调用代理方法, 返回需要实现缩放的子控件

    (4) 设置缩放比例

 2.补充: 跟缩放相关的代理方法

 (1)  - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view

 (2)  - (void)scrollViewDidZoom:(UIScrollView *)scrollView

三. 代码部分 (可以直接复制使用)

 

#import "ThirdViewController.h"
#import "Masonry.h"

@interface ThirdViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView  * scrollView;

@property (nonatomic, strong) UIImage       * image;

@property (nonatomic, strong) UIImageView   * imageView;


@end

@implementation ThirdViewController

#pragma mark - 生命周期
#pragma mark viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self basicSetting];
    
    [self addscrollView];
    
    [self addImageView];
}

#pragma mark - 系统代理

#pragma mark 返回需要缩放的子控件
-  (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

#pragma mark - 实现方法
#pragma mark 基本设置
- (void)basicSetting
{
    self.title = @"UIScrollView-图片放大";
}

- (void)addscrollView
{
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)addImageView
{
    [self.scrollView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.scrollView);
        make.width.mas_equalTo(self.view.bounds.size.width);
        make.top.mas_equalTo(self.scrollView);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.height);
    }];
}

#pragma mark - setter & getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = [[UIScrollView alloc] init];
        self.scrollView.backgroundColor = [UIColor orangeColor];
        
        // 设置最大拉伸比例
        self.scrollView.maximumZoomScale = 2.0f;
        
        // 设置最小缩放比例
        self.scrollView.minimumZoomScale = 0.5f;
        
        // 设置scrollview 的滚动范围和图片真实尺寸一致
        self.scrollView.contentSize = self.image.size;
        
        self.scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIImage *)image
{
    if (!_image)
    {
        self.image = [UIImage imageNamed:@"红妹子"];
    }
    return _image;
}

- (UIImageView *)imageView
{
    if (!_imageView)
    {
        self.imageView = [[UIImageView alloc] init];
        self.imageView.image = self.image;
    }
    return _imageView;
}

@end

 

iOS-UIScrollView-图片缩放

标签:

原文地址:http://www.cnblogs.com/mancong/p/5547291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!