UIScrollView のタッチイベントを取得する
UIScrollView のタッチイベントを取得するには UIScrollView のサブクラスを作って、タッチイベント関連のメソッドをオーバーライドします。大体の場合、フリックやピンチとは別でタッチイベントを取りたいと思うので、その方法を。
まずは UIScrollView のサブクラスを定義します。名前は MyScrollview とでも名付けます。次に、以下のように touchesEnded メソッドをオーバーライドします。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.dragging) { [self.nextResponder touchesEnded: touches withEvent:event]; } [super touchesEnded: touches withEvent: event]; }
!self.dragging と書く事で、ドラッグされなかった場合のみイベントが呼び出されるのがポイントです。
親クラス側では次のように記述します。
- (void)viewDidLoad { [super viewDidLoad]; scrollView = [[MyScrollView alloc] initWithFrame:self.view.bounds]; scrollView.maximumZoomScale = 5.0f; scrollView.minimumZoomScale = 0.5f; scrollView.clipsToBounds = YES; scrollView.contentSize = imageView.bounds.size; scrollView.delegate = self; scrollView.userInteractionEnabled = YES; [scrollView setScrollEnabled:YES]; [self.view addSubview:scrollView]; } // ピンチのイベント - (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView { for(id subview in self.scrollView.subviews) { if([subview isKindOfClass:[UIImageView class]]) { return subview; } } return nil; } // タッチのイベント -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touch!!"); }