1 #import "RootViewController.h"
2
3 @interface RootViewController ()<UIScrollViewDelegate>
4 {
5 UIImageView *imageView;
6 UILabel *scaleRatioLabel;// 显示倍率用的Label
7 }
8 @property (nonatomic, strong)UIScrollView *scrollView;
9
10 @end
11
12 @implementation RootViewController
13
14 - (void)dealloc
15 {
16 self.scrollView = nil;
17 }
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
22 self.scrollView.delegate = self;
23 [self.view addSubview:self.scrollView];
24 imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1-3.jpg"]];
25 [imageView setCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.height/2.0)];
26 [self.scrollView addSubview:imageView];
27 //内容大小与图片大小一致
28 self.scrollView.contentSize = imageView.frame.size;
29 // 最小缩放比例
30 self.scrollView.minimumZoomScale = 0.2f;
31 // 最大缩放比例
32 self.scrollView.maximumZoomScale = 5.0f;
33
34 // 用来显示倍率的Label
35 scaleRatioLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2.0 - 25, [UIScreen mainScreen].bounds.size.height/2.0 - 12.5, 50, 25)];
36 [scaleRatioLabel setBackgroundColor:[UIColor clearColor]];
37 [self.view addSubview:scaleRatioLabel];
38
39 }
40
41 #pragma mark - UIScrollViewDelegate
42 // 设置要缩放的控件
43 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
44 {
45 return imageView;
46 }
47
48 // 处理结束缩放事件
49 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
50 {
51 [self.view bringSubviewToFront:scaleRatioLabel];
52 [scaleRatioLabel setAlpha:0.6f];
53 [scaleRatioLabel setBackgroundColor:[UIColor lightGrayColor]];
54 scaleRatioLabel.text = [NSString stringWithFormat:@" x%.1f",scale];
55
56 [UIView transitionWithView:scaleRatioLabel duration:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
57 scaleRatioLabel.alpha = 0.0f;
58 } completion:nil];
59 }
60
61
62 @end