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
- TapToZoom例子学习
- 通过例子学习Lua
- Hibernate3 参考手册 例子学习
- java接口学习例子
- C#,学习webserver例子
- grizzly学习例子
- DWR入门教程--学习例子
- java线程学习例子
- oop学习例子
- oop学习例子2
- 微软Dinnernow例子 学习
- 链表学习例子
- 通过例子学习Lua
- 一个学习例子
- ssh学习例子
- 通过例子学习Lua
- iPhone基础学习例子
- awk学习--例子篇
- Unable to read TLD "META-INF/c.tld" from JAR file ********
- 小心肝
- struts中的异常应用实践
- Table间隔色以及选择高亮(和动态切换数据)
- 海量数据处理面试题
- TapToZoom例子学习
- ubuntu加载硬盘分区
- android内核字符驱动设备实战之----------应用框架层aidl服务编程篇
- cd命令
- ProtoBuf开发者指南
- 別人的sql
- 实现简易而强大的游戏AI——FSM,有限状态机
- 求助:ACtiveX控件如何获取JScript对象
- 箱排序(Bin Sort)