我正在查看来自 ScrollViewSuite 的 Apple 示例代码。相比之下,我一定做错了什么。在他们的 RootViewController 中,他们这样做:
- (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];
}
在 IB 中,他们将 imageView 作为 imageScrollView 的子视图。我的 .xib 的设置方式相同。我把这段代码放在我的 viewDidLoad 而不是 loadView 中。当我双击我的 imageView 时,我没有接到任何电话。我想知道为什么。这是他们所做的我的版本:
- (void)viewDidLoad {
[self updateOnScreenImages]; // this is where my imageView.image property gets set
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTapsRequired = 2;
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
[doubleTap setNumberOfTouchesRequired:2];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:twoFingerTap];
[doubleTap release];
[twoFingerTap release];
}
与 Apple 所做的相比,我做错了什么吗?逻辑似乎和我很相似。但是当我双击时我没有收到我的回调。我也尝试将手势添加到 self.scrollView,但这也不起作用。想法?谢谢!