IOS控件-UIScrollView简介(三)–缩放图片

在IOS中,
我们可以通过在UIScrollView插入一个UIImageView对象,来显示一张图片,

当图片过大时,我们可以通过UIScrollView的缩放功能,对图片进行放大查看

具体操作方式如下代码所示:




//  ViewController.h 
//  Copyright © 2016年 maomao365.com. All rights reserved.
 
#import < UIKit / UIKit.h > 


#import "refreshControl.h"


@interface ViewController : UIViewController
{ 
    UIScrollView * _uiScrollViewTest;
    UIImageView  * _uiImageViewTest;
}
 
@end


//  ViewController.m
//  Copyright © 2016年 maomao365.com. All rights reserved.
#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController
 
//uiscrollview缩放时调用,并返回uiscrollview中插入的对象,方便uiscrollview重绘
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    //NSLog(@"准备缩放,在模拟器中按住option双指移动时触发");
    return _uiImageViewTest; //返回uiscrollView中的插入对象
}

//结束缩放
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    NSLog(@"结束缩放--%f",scale);
}


-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"正在缩放时!");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIScrollView *uiScrollViewTest = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 30,
                                      600, 500)];
    uiScrollViewTest.delegate = self; //设置代理 方法uiscrollview调用本地方法
    uiScrollViewTest.maximumZoomScale = 3.0; //最大缩放倍数
    uiScrollViewTest.minimumZoomScale = 0.1; //最小缩放倍数
    
    UIImageView *uiImageViewTest = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 600, 500)];
    uiImageViewTest.image = [UIImage imageNamed:@"2.png"];
    [uiScrollViewTest addSubview:uiImageViewTest];
    _uiImageViewTest = uiImageViewTest;
    
    [self.view addSubview:uiScrollViewTest];
    
}

相关阅读:
IOS控件-UIScrollView简介(二) –按钮移动
IOS控件-UIScrollView简介(一) –控件简介