TapToZoom例子学习

来源:互联网 发布:中国黑人数量知乎 编辑:程序博客网 时间:2024/06/05 05:20

TapToZoom可以双击放大图片,支持两指拉伸缩小图片。


RootViewController.h

@interface RootViewController : UIViewController <UIScrollViewDelegate> {    UIScrollView *imageScrollView;UIImageView *imageView;}@property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;@property (nonatomic, retain) IBOutlet UIImageView *imageView;@end

RootViewController.m

引入头文件/申明

#import "RootViewController.h"#define ZOOM_VIEW_TAG 100#define ZOOM_STEP 1.5
@synthesize imageScrollView, imageView;

载入视图方法
- (void)loadView {    [super loadView];        // set the tag for the image view    [imageView setTag:ZOOM_VIEW_TAG];        // add gesture recognizers to the image view    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(handleTwoFingerTap:)];        [doubleTap setNumberOfTapsRequired:2];    [twoFingerTap setNumberOfTouchesRequired:2];        [imageView addGestureRecognizer:singleTap];    [imageView addGestureRecognizer:doubleTap];    [imageView addGestureRecognizer:twoFingerTap];        [singleTap release];    [doubleTap release];    [twoFingerTap release];        // calculate minimum scale to perfectly fit image width, and begin at that scale    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;    [imageScrollView setMinimumZoomScale:minimumScale];    [imageScrollView setZoomScale:minimumScale];}

UIScrollViewDelegate的方法
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {    return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];}

#pragma mark TapDetectingImageViewDelegate methods
单指无指令
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {    // single tap does nothing for now}
单指双击放大
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {    // double tap zooms in    float newScale = [imageScrollView zoomScale] * ZOOM_STEP;    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];    [imageScrollView zoomToRect:zoomRect animated:YES];}
两指单击缩小
- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {    // two-finger tap zooms out    float newScale = [imageScrollView zoomScale] / ZOOM_STEP;    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];    [imageScrollView zoomToRect:zoomRect animated:YES];}

#pragma mark Utility methods
放大缩小的委托在这。改变一次放大缩小的量
- (CGRect)zoomRectForScale:(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 = [imageScrollView frame].size.height / scale;    zoomRect.size.width  = [imageScrollView 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;}
demo下载http://download.csdn.net/detail/gwh111/4982192





原创粉丝点击