码迷,mamicode.com
首页 > 移动开发 > 详细

iOS & Objective-C UIScrollView 图片缩放+居中

时间:2015-10-28 22:44:41      阅读:458      评论:0      收藏:0      [点我收藏+]

标签:

(面前横着图形学实验的deadline 我居然搞这个。。

在将图片居中的地方坑了好久,从 NSLog 输出的内容可以看出使用 ScrollView 大概的函数调用流程 略

最后是在 (void)scrollViewDidZoom:(UIScrollView *)scrollView 函数中更新 imageView.frame.origin,就是image在scrollView里的偏移。

 

  • 下面这条要靠前写,不然设置zoomScale就不会生效

    self.scrollView.delegate = self;

  •  minimumZoomScale 设置成实际大小或者适应屏幕大小

 

技术分享
 1 //
 2 //  ViewController.h
 3 //  testZoom
 4 //
 5 //  Created by my on 10/27.
 6 //  Copyright (c) 2015年 my. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface ViewController : UIViewController <UIScrollViewDelegate>
12 
13 
14 @end
ViewController.h
技术分享
  1 //
  2 //  ViewController.m
  3 //  testZoom
  4 //
  5 //  Created by my on 10/27.
  6 //  Copyright (c) 2015年 my. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()
 12 
 13 @property (nonatomic, strong) UIScrollView *scrollView;
 14 
 15 @property (nonatomic, strong) UIImageView *imageView;
 16 
 17 @end
 18 
 19 @implementation ViewController
 20 
 21 static int cnt = 0;
 22 
 23 - (void)viewDidLoad {
 24     [super viewDidLoad];
 25     
 26     self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 27     self.scrollView.backgroundColor = [UIColor lightGrayColor];
 28     [self.view addSubview:self.scrollView];
 29     self.scrollView.delegate = self;
 30 //    NSLog(@"%f %f viewDidLoad() scrollView.bounds", self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
 31 
 32     
 33 //    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"19m.jpg"]];
 34     self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Maggie_1.jpeg"]];
 35 
 36     [self.scrollView addSubview:self.imageView];
 37     self.scrollView.contentSize = self.imageView.bounds.size;
 38     
 39 //    NSLog(@"%f %f viewDidLoad() scrollView.contentSize", self.scrollView.contentSize.width, self.scrollView.contentSize.height);
 40     
 41     float xRate = self.scrollView.bounds.size.width / self.imageView.bounds.size.width;
 42     float yRate = self.scrollView.bounds.size.height / self.imageView.bounds.size.height;
 43     self.scrollView.minimumZoomScale = MIN(MIN(xRate, yRate), 1);
 44     self.scrollView.maximumZoomScale = 4.0;
 45     self.scrollView.bouncesZoom = YES;
 46 
 47     self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
 48     self.imageView.center = self.scrollView.center;
 49 
 50     
 51 //    NSLog(@"%d from viewDidLoad()", ++cnt);
 52     
 53 //    NSLog(@"----------------------------");
 54 //    CGRect tmp = self.imageView.frame;
 55 //    NSLog(@"image view");
 56 //    NSLog(@"x = %f, y = %f", tmp.origin.x, tmp.origin.y);
 57 //    NSLog(@"w = %f, h = %f", tmp.size.width, tmp.size.height);
 58 //    NSLog(@"center = (%f,%f)", self.imageView.center.x, self.imageView.center.y);
 59 //    NSLog(@"----------------------------");
 60     
 61 }
 62 
 63 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
 64 {
 65 //    NSLog(@"%d from ZoomingInScrollView()", ++cnt);
 66     return self.imageView;
 67 }
 68 
 69 
 70 /* scrollview将要开始Zooming */
 71 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
 72 //    NSLog(@"%d from BeginZooming()", ++cnt);
 73 }
 74 
 75 /* scrollview已经发生了Zoom事件 */
 76 - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
 77 //    NSLog(@"%d from DidZoom()", ++cnt);
 78     
 79     if(self.imageView.frame.size.width >= self.scrollView.frame.size.width && self.imageView.frame.size.height >= self.scrollView.frame.size.height){
 80         self.imageView.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
 81     }
 82     if(self.imageView.frame.size.width < self.scrollView.frame.size.width){
 83         self.imageView.frame = CGRectMake((self.scrollView.frame.size.width-self.imageView.frame.size.width)/2, self.imageView.frame.origin.y, self.imageView.frame.size.width, self.imageView.frame.size.height);
 84     }
 85     if(self.imageView.frame.size.height < self.scrollView.frame.size.height){
 86         self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, (self.scrollView.frame.size.height-self.imageView.frame.size.height)/2, self.imageView.frame.size.width, self.imageView.frame.size.height);
 87     }
 88 
 89 }
 90 
 91 /* scrollview完成Zooming */
 92 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
 93 //    NSLog(@"%d from DidEndZooming()", ++cnt);
 94 //    
 95 //    NSLog(@"----------------------------");
 96 //    CGSize size = self.scrollView.contentSize;
 97 //    NSLog(@"Content size of scroll view contentSize");
 98 //    NSLog(@"w = %f, h = %f", size.width, size.height);
 99 //    NSLog(@"----------------------------");
100 //    NSLog(@"zoomscale = %f", self.scrollView.zoomScale);
101 //    NSLog(@"----------------------------");
102 //    CGRect boundsOfScrollView = self.scrollView.frame;
103 //    NSLog(@"Bounds of scroll view");
104 //    NSLog(@"x = %f, y = %f", boundsOfScrollView.origin.x, boundsOfScrollView.origin.y);
105 //    NSLog(@"w = %f, h = %f", boundsOfScrollView.size.width, boundsOfScrollView.size.height);
106 //    NSLog(@"----------------------------");
107 //    CGRect tmp = self.imageView.frame;
108 //    NSLog(@"image view");
109 //    NSLog(@"x = %f, y = %f", tmp.origin.x, tmp.origin.y);
110 //    NSLog(@"w = %f, h = %f", tmp.size.width, tmp.size.height);
111 //    NSLog(@"center = (%f,%f)", self.imageView.center.x, self.imageView.center.y);
112 //    NSLog(@"----------------------------");
113 }
114 
115 - (void)didReceiveMemoryWarning {
116     [super didReceiveMemoryWarning];
117     // Dispose of any resources that can be recreated.
118 }
119 
120 @end
ViewController.m

 

iOS & Objective-C UIScrollView 图片缩放+居中

标签:

原文地址:http://www.cnblogs.com/heiqi2piao/p/4918804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!