我的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
答案 0 :(得分:0)
默认情况下,ImageViews默认不响应用户交互,你必须设置它:
imageView.userInteractionEnabled = YES;
答案 1 :(得分:0)
您需要确保单击识别器等待双击失败。
[singleTap requireGestureRecognizerToFail:doubleTap];
答案 2 :(得分:0)
必须覆盖-loadView
才能初始化self.view
- 没有别的。由于我看不到初始化self.view
的代码,我认为你的视图来自nib文件,因此你不能覆盖-loadView
方法,或者如果超类负责这个初始化,你不必覆盖它并调用[super loadView]
,但你放在那里的代码完全在错误的地方。将所有逻辑移至viewDidLoad
除以下行:
// 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];
这些放在viewWillAppear:
中,因为这是正确设置视图层次结构几何的位置,并且您可以拥有准确的frame
属性。
答案 3 :(得分:0)
@interface RootViewController(UtilityMethods) - (CGRect)zoomRectForScale :( float)scale withCenter:(CGPoint)center; @end
@implementation RootViewController
@synthesize imageScrollView,imageView,mySlider;
(IBAction)sliderValueChanged:(UISlider *)sender { mySlider.value = [NSString stringWithFormat:imageScrollView。[sender value]];
// mySlider.value = [NSString stringWithFormat:@“%。1f”,[发送者值]];
// myTextField.text = [NSString stringWithFormat:@“%。1f”,[发送者值]]; }
(void)loadView { [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 release]; [doubleTap发布]; [twoFingerTap发布];
//计算最小比例以完全适合图像宽度,并从该比例开始 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发布]; [imageView发布]; [super dealloc]; }
/ <强> * ** * ** * ** * ** * ** * ** * * 注 * ** * ** * ** * ** * ** * ** * * 强> / / *以下委托方法适用于zoomToRect中的已知错误:animated: / / 在3.0之后的下一个版本中,将不再需要这种解决方法 / / <强> 强> 的 ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** 强> / - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { [scrollView setZoomScale:scale + 0.01 animated:NO]; [scrollView setZoomScale:scale animated:NO]; }
(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer { //单击现在什么都不做 }
(void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer { //双击放大 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 { //双指轻敲缩小 float newScale = [imageScrollView zoomScale] / ZOOM_STEP; CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; [imageScrollView zoomToRect:zoomRect animated:YES]; }
(CGRect)zoomRectForScale :( float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
//缩放矩形位于内容视图的坐标中。 //缩放比例为1.0时,它将是imageScrollView边界的大小。 //随着缩放比例的减小,可见的内容越多,矩形的大小就越大。 zoomRect.size.height = [imageScrollView frame] .size.height / scale; 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);
return zoomRect; }
@end