iOS & Objective-C UIScrollView 圖片縮放+置中

來源:互聯網
上載者:User

標籤:

(面前橫著圖形學實驗的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 圖片縮放+置中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.