使用scrollview实现图片放大缩小
1 #import "ViewController.h" 2 #import <UIKit/UIMenuController.h> 3 4 @interface ViewController ()<UIScrollViewDelegate> 5 @property (strong, nonatomic) UIScrollView *scrView; 6 7 @property (nonatomic,strong)UIImageView *imgv; 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 //[[UILabel appearance] setTextColor:[UIColor yellowColor]]; 15 self.scrView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; 16 self.scrView.contentSize = CGSizeMake(800, 1800); 17 self.scrView.backgroundColor = [UIColor yellowColor]; 18 [self.view addSubview:self.scrView]; 19 20 21 // 设置放大缩小的比例 22 self.scrView.multipleTouchEnabled = YES;//打开多指触控 23 self.scrView.maximumZoomScale = 2.0; 24 self.scrView.minimumZoomScale = 0.5; 25 self.scrView.zoomScale = 3.0; 26 27 self.scrView.delegate = self; 28 29 UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(150, 150, 150, 150)]; 30 imgv.backgroundColor = [UIColor grayColor]; 31 [self.scrView addSubview:imgv]; 32 self.imgv = imgv; 33 34 } 35 36 // *** 必须实现的代理方法 37 38 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ 39 return self.imgv; 40 } 41 // ***是图片保持位于视图中心 42 43 -(void)scrollViewDidZoom:(UIScrollView *)scrollView{ 44 self.imgv.center = self.view.center; 45 }