首页 > 其他 > 详细

UIScrollView 实现比例缩放

时间:2015-07-14 22:25:54      阅读:174      评论:0      收藏:0      [点我收藏+]
 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

 

UIScrollView 实现比例缩放

原文:http://www.cnblogs.com/lantu1989/p/4646637.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!