转自:http://blog.csdn.net/nerohoop/article/details/7011679
Basic Zooming Using the Pinch Gestures
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
Supporting the Pinch Zoom Gestures
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.scrollView.minimumZoomScale=0.5;
- self.scrollView.maximumZoomScale=6.0;
- self.scrollView.contentSize=CGSizeMake(1280, 960);
- self.scrollView.delegate=self;
- }
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
}
Zooming Programmatically
1.The setZoomScale:animated: sets the current zoom scale to the specified value. The zoomToRect:animated: method zooms the content in order to fill the specified rectangle. The value must be within the range specified by the minimumZoomScale and maximumZoomScale range.
2. The zoomToRect:animated: method zooms the content in order to fill the specified rectangle. As withsetZoomScale:animated: this method it has an animated parameter that determines whether the change in location and zoom results in animation taking place.
- <SPAN style="FONT-SIZE: 18px">- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {
- CGRect zoomRect;
- // The zoom rect is in the content view's coordinates.
- // At a zoom scale of 1.0, it would be the size of the
- // imageScrollView's bounds.
- // As the zoom scale decreases, so more content is visible,
- // the size of the rect grows.
- zoomRect.size.height = scrollView.frame.size.height / scale;
- zoomRect.size.width = scrollView.frame.size.width / scale;
- // choose an origin so as to get the right center.
- zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
- zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
- return zoomRect;
- }
- }</SPAN>
<span style="font-size:18px;">- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// The zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the
// imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible,
// the size of the rect grows.
zoomRect.size.height = scrollView.frame.size.height / scale;
zoomRect.size.width = scrollView.frame.size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
}</span>
Informing the Delegate that the Zoom is Finished
When the user has completed the zoom pinch gestures or the programmatic zooming of the scroll view is completed, the UIScrollView delegate is informed by receiving a scrollViewDidEndZooming:withView:atScale: message.Zooming by Tapping
Implementing the Touch-Handling Code
- <SPAN style="FONT-SIZE: 18px; FONT-WEIGHT: normal">- (void)loadView
- {
- [super loadView];
- self.imageView.tag = kZOOM_VIEW_TAG;
- self.imageView.userInteractionEnabled = YES;
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
- UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
- UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTowFingerTap:)];
- doubleTap.numberOfTapsRequired = 2;
- twoFingerTap.numberOfTouchesRequired = 2;
- twoFingerTap.numberOfTapsRequired = 2;
- [self.imageView addGestureRecognizer:singleTap];
- [self.imageView addGestureRecognizer:doubleTap];
- [self.imageView addGestureRecognizer:twoFingerTap];
- [singleTap release];
- [doubleTap release];
- [twoFingerTap release];
- self.scrollView.delegate = self;
- self.scrollView.contentSize = self.imageView.frame.size;
- self.view = self.scrollView;
- [self.scrollView addSubview:self.imageView];
- float minimumScale = self.scrollView.frame.size.width / self.imageView.frame.size.width;
- self.scrollView.minimumZoomScale = minimumScale;
- self.scrollView.maximumZoomScale = 1;
- self.scrollView.zoomScale = minimumScale;
- }</SPAN>
<span style="font-size:18px;FONT-WEIGHT: normal">- (void)loadView
{
[super loadView];
self.imageView.tag = kZOOM_VIEW_TAG;
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTowFingerTap:)];
doubleTap.numberOfTapsRequired = 2;
twoFingerTap.numberOfTouchesRequired = 2;
twoFingerTap.numberOfTapsRequired = 2;
[self.imageView addGestureRecognizer:singleTap];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[singleTap release];
[doubleTap release];
[twoFingerTap release];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.frame.size;
self.view = self.scrollView;
[self.scrollView addSubview:self.imageView];
float minimumScale = self.scrollView.frame.size.width / self.imageView.frame.size.width;
self.scrollView.minimumZoomScale = minimumScale;
self.scrollView.maximumZoomScale = 1;
self.scrollView.zoomScale = minimumScale;
}</span>