// // ViewController.m // 图片放大 // // Created by 键盘上的舞者 on 15/5/21. // Copyright (c) 2015年 键盘上的舞者. All rights reserved. // #import "ViewController.h" @interface ViewController (){ UIImageView *_imageView; } @end @implementation ViewController - (void)viewDidLoad { // Do any additional setup after loading the view, typically from a nib. //添加一个scrollView UIScrollView *scrollView=[[UIScrollView alloc] init]; scrollView.frame=CGRectMake(0, 0, 320, 568); //scrollView.backgroundColor=[UIColor grayColor]; [self.view addSubview:scrollView]; //添加一个图片 _imageView=[[UIImageView alloc] init]; _imageView.image=[UIImage imageNamed:@"1.jpg"]; CGFloat imgW=_imageView.image.size.width; CGFloat imgH=_imageView.image.size.height; _imageView.frame=CGRectMake(0, 0, imgW,imgH); [scrollView addSubview:_imageView]; //设置scrollView的内容宽度和高度 scrollView.contentSize=_imageView.image.size; scrollView.contentOffset=CGPointMake(250, 250); //设置是否显示水平或者垂直的滚动条 scrollView.showsHorizontalScrollIndicator=NO; scrollView.showsVerticalScrollIndicator=NO; //设置偏移 //细节:设置了contentOffset会忽略掉contentSize scrollView.contentOffset=CGPointMake(100, 100); //弹簧效果 scrollView.bounces=YES; //添加代理 scrollView.delegate=self; //设置缩放的比例 scrollView.maximumZoomScale=2.0; scrollView.minimumZoomScale=0.2; } -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageView; } @end