我的UIImage没有用以下代码缩放。可以识别水龙头,但是变焦无法使用。它基本上是来自iOS开发人员站点的taptozoom代码,其中的导航控制器位于UIScrollView和UIImageView的后面。此TapToZoom代码无需导航控制器即可使用。图像延伸到顶部的导航栏是否存在问题?
#import "Map.h"
#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 1.5
@interface Map (UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end
@implementation Map
@synthesize imageScrollView, imageView;
- (void)loadView {
//imageScrollView.userInteractionEnabled = YES;
// [self.imageView setUserInteractionEnabled:YES];
//[self.imageView setMultipleTouchEnabled:YES];
NSLog(@"beginning of loadView in map.m");
[super loadView];
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
//imageScrollView.scrollEnabled = NO;
// 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]; //added self to fix subview
[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];
}
- (void)viewDidUnload {
self.imageScrollView = nil;
self.imageView = nil;
}
- (void)dealloc {
[imageScrollView release];
[imageView release];
[super dealloc];
}
#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];
}
/************************************** NOTE **************************************/
/* The following delegate method works around a known bug in zoomToRect:animated: */
/* In the next release after 3.0 this workaround will no longer be necessary */
/**********************************************************************************/
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[scrollView setZoomScale:scale+0.01 animated:NO];
[scrollView setZoomScale:scale animated:NO];
}
#pragma mark TapDetectingImageViewDelegate methods
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
// single tap does nothing for now
NSLog(@"single tap detected");
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
// double tap zooms in
NSLog(@"beginning handleDoubleTap to zoom");
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 UIScroll Subview
#pragma mark Utility methods
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
NSLog(@"zoomRectForScale");
// 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);
NSLog(@"end of zoom rect");
return zoomRect;
}
@end
最佳答案
导入“RootViewController.h”
定义ZOOM_VIEW_TAG 100
定义ZOOM_STEP 1.5
@interface RootViewController(UtilityMethods)
-(CGRect)zoomRectForScale:(float)scale与Center:(CGPoint)center;
@结束
@implementation RootViewController
@synthesize imageScrollView,imageView,mySlider;
mySlider.value = [NSString stringWithFormat:imageScrollView。[发送者值]];
//mySlider.value = [NSString stringWithFormat:@“%.1f”,[发送者值]];
//myTextField.text = [NSString stringWithFormat:@“%.1f”,[发送者值]];
[super loadView];
//设置图片视图的标签
[imageView setTag:ZOOM_VIEW_TAG];
//将手势识别器添加到图像视图
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发布];
[doubleTap发布];
[twoFingerTap发行];
//计算最小比例以完全适合图像宽度,然后从该比例开始
float minimumScale = [imageScrollView frame] .size.width / [imageView frame] .size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
self.imageScrollView = nil;
self.imageView = nil;
[imageScrollView发布];
[imageView发布];
[super dealloc];
实用标记UIScrollViewDelegate方法
返回[imageScrollView viewWithTag:ZOOM_VIEW_TAG];
/ ******************** 注意 ******************** /
/ *以下委托方法可解决zoomToRect:animated中的已知错误:/
/在3.0之后的下一个版本中,将不再需要此解决方法/
/ ************************************************** * /
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[scrollView setZoomScale:scale + 0.01 animation:NO];
[scrollView setZoomScale:缩放动画:否];
}
实用标记mark TapDetectingImageViewDelegate方法
//暂时不执行任何操作
//双击放大
float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[imageScrollView zoomToRect:zoomRect动画:是];
//两指轻按即可缩小
float newScale = [imageScrollView zoomScale] / ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[imageScrollView zoomToRect:zoomRect动画:是];
实用标记实用程序方法
CGRect zoomRect;
//缩放矩形位于内容视图的坐标中。
//缩放比例为1.0时,它将为imageScrollView边界的大小。
//随着缩放比例的减小,可以看到更多内容,矩形的大小也随之增大。
zoomRect.size.height = [imageScrollView框架] .size.height /比例尺;
zoomRect.size.width = [imageScrollView frame] .size.width / scale;
//选择一个原点以获得正确的中心。
zoomRect.origin.x = center.x-(zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y-(zoomRect.size.height / 2.0);
返回zoomRect;
@结束
关于iphone - 双击图像无响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8044476/