UIScrollView滚动视图结合UIImageView图像视图实现图像缩放效果,包括监测手势识别器(Gesture Recognizer)等等。
本范例项目,需要使用前面《一步一步学习iOS 5 编程(第三版) 》教程中的很多知识,包括导航控制器、表视图、联线(Segue)、UIScrollView和UIImageView等等。
开发环境:Xcode 4.5 + iOS 6 iPhone 模拟器
示例App – PhotoBrowser 应用程序 – 滚动图像和手势识别功能–最终运行界面如下所示:
在示例代码中,我们手动编写代码,添加了2个触摸手势识别器的支持。
// 添加手势识别器 - 一个手指轻按 2 次
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
// 添加手势识别器 - 二个手指轻按 1 次
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
另外,我们设置视图控制器为UIScrollView 的delegate。同时在视图控制器中,实现UIImageView的viewForZooming 委托方法:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
该方法直接返回UIImageView 对象实例。
这样,由于UIScrollView 负责处理图像的张合手势操作,我们不必在代码中手工编写张合手势(Pinch Gesture Recognizer)的方法了。通过张合手指,我们通用可以在示例程序中实现图像的缩放。
在iOS 模拟器中如何实现张合手势(Pinch Gesture)呢?
按住Option键,当iPhone 模拟器上出现2点时,按住鼠标键拖拉2点的距离,实现模拟手指的张合操作。
实现图像缩放及其手势识别器的部分核心代码,如下所示。具体的操作和实现过程,参考我们的电子书《一步一步学习iOS 6 编程》。
关于UIScrollView 滚动视图的一些用法,还可以参考如下文章:
iOS 6编程(18)-UIScrollView滚动视图和UIPageControl分页控件的简单应用
//
// ZoomImageViewController.m
// PhotoBrowser
//
// Created by EntLib.com on 12-10-11.
// Copyright (c) 2012年 EntLib.com. All rights reserved.
//
#import “ZoomImageViewController.h”
#define ZOOM_STEP 1.5
@interface ZoomImageViewController ()
- (void)scrollViewDoubleTapped: (UITapGestureRecognizer *)recognizer;
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer *)recognizer;
@end
@implementation ZoomImageViewController
@synthesize scrollView;
@synthesize imageView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 通过代码添加image view
UIImage *image = [UIImage imageNamed:@"90s-girl.jpg"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.imageView];
self.scrollView.contentSize = image.size;
// 添加手势识别器 – 一个手指轻按 2 次
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
// 添加手势识别器 – 二个手指轻按 1 次
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.scrollView.maximumZoomScale = 2.0f;
self.scrollView.zoomScale = minScale;
float minimumScale = [scrollView frame].size.width / [imageView frame].size.width;
[scrollView setMinimumZoomScale:minimumScale];
[scrollView setZoomScale:minimumScale];
}
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer *)recognizer{
CGPoint pointInView = [recognizer locationInView:recognizer.view];
CGFloat newZoomScale = self.scrollView.zoomScale * ZOOM_STEP;
CGSize scrollViewSize = self.scrollView.frame.size;
CGFloat width = scrollViewSize.width / newZoomScale;
CGFloat height = scrollViewSize.height / newZoomScale;
CGFloat x = pointInView.x – (width / 2.0f);
CGFloat y = pointInView.y – (height / 2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, width, height);
[self.scrollView zoomToRect:rectToZoomTo animated:YES];
}
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer *)recognizer{
CGFloat newZoomScale = self.scrollView.zoomScale / ZOOM_STEP;
newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
[self.scrollView setZoomScale:newZoomScale animated:YES];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
@end