iOS开发之-Scroll View Programming 2-Zooming

转自:http://blog.csdn.net/nerohoop/article/details/7011679

 

Basic Zooming Using the Pinch Gestures


 

UIImageView 
- (id)initWithImage:(UIImage *)image
This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default.

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


1. To support zooming, you must set a delegate for your scroll view. That delegate class must implement the viewForZoomingInScrollView: method and return the view to zoom. 

2. To specify the amount the user can zoom, you set the values of the minimumZoomScale and maximumZoomScale properties, both of which are initially set to 1.0. 

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     self.scrollView.minimumZoomScale=0.5;  
  4.     self.scrollView.maximumZoomScale=6.0;  
  5.     self.scrollView.contentSize=CGSizeMake(1280, 960);  
  6.     self.scrollView.delegate=self;  
  7. }  
- (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.


  1. <SPAN style="FONT-SIZE: 18px">- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {  
  2.    
  3.     CGRect zoomRect;  
  4.    
  5.     // The zoom rect is in the content view's coordinates.  
  6.     // At a zoom scale of 1.0, it would be the size of the  
  7.     // imageScrollView's bounds.  
  8.     // As the zoom scale decreases, so more content is visible,  
  9.     // the size of the rect grows.  
  10.     zoomRect.size.height = scrollView.frame.size.height / scale;  
  11.     zoomRect.size.width  = scrollView.frame.size.width  / scale;  
  12.    
  13.     // choose an origin so as to get the right center.  
  14.     zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);  
  15.     zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);  
  16.    
  17.     return zoomRect;  
  18. }  
  19. }</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


  1. <SPAN style="FONT-SIZE: 18px; FONT-WEIGHT: normal">- (void)loadView  
  2. {  
  3.     [super loadView];  
  4.   
  5.     self.imageView.tag = kZOOM_VIEW_TAG;  
  6.     self.imageView.userInteractionEnabled = YES;  
  7.       
  8.     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  
  9.     UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];  
  10.     UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTowFingerTap:)];  
  11.       
  12.     doubleTap.numberOfTapsRequired = 2;  
  13.       
  14.     twoFingerTap.numberOfTouchesRequired = 2;  
  15.     twoFingerTap.numberOfTapsRequired = 2;  
  16.       
  17.     [self.imageView addGestureRecognizer:singleTap];  
  18.     [self.imageView addGestureRecognizer:doubleTap];  
  19.     [self.imageView addGestureRecognizer:twoFingerTap];  
  20.       
  21.     [singleTap release];  
  22.     [doubleTap release];  
  23.     [twoFingerTap release];  
  24.       
  25.     self.scrollView.delegate = self;  
  26.     self.scrollView.contentSize = self.imageView.frame.size;  
  27.       
  28.     self.view = self.scrollView;  
  29.     [self.scrollView addSubview:self.imageView];  
  30.       
  31.     float minimumScale =  self.scrollView.frame.size.width / self.imageView.frame.size.width;  
  32.     self.scrollView.minimumZoomScale = minimumScale;  
  33.     self.scrollView.maximumZoomScale = 1;  
  34.     self.scrollView.zoomScale = minimumScale;  
  35. }</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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值