2011-05-03 2 views
6

나는 UIScrollView에서 탭 한 UIImageView를 간단하게 잡으려고합니다.'n'UIImageViews가 포함 된 UIScrollView의 단일 탭 감지

저는 웹에서 위의 두 가지 방법을 발견했습니다.

첫 번째 방법 : scrollviewer에 추가하기 전에 uiimageview에서 탭 제스처를 만듭니다.

이 방법은 저에게 효과적이지 않습니다. handleSingleTap 메서드는 결코 호출되지 않습니다.

내가 잘못하고있는 것/왜 이것이 작동하지 않는지에 대한 아이디어가 없습니다.

UITapGestureRecognizer *singleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
    action:@selector(handleSingleTap:)]; 
     singleTap.numberOfTapsRequired = 1; 
     [imageView addGestureRecognizer:singleTap]; 
     [singleTap release]; 

     [framesSourceScrollview addSubview:imageView]; 
     [imageView release]; 

      - (void)handleSingleTap:(UIGestureRecognizer *)sender 
      { 
       NSLog(@"image tapped!!!"); 
      } 

두번째 방법 :이 방법을 사용

@interface SingleTapScrollViewer : UIScrollView { 
} 
@end 

@implementation SingleTapScrollViewer 

- (id)initWithFrame:(CGRect)frame 
{ 
    return [super initWithFrame:frame]; 
} 

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{ 
    // If not dragging, send event to next responder 
    if (!self.dragging) 
     [self.nextResponder touchesEnded: touches withEvent:event]; 
    else 
     [super touchesEnded: touches withEvent: event]; 
} 
@end 

In the ViewController 

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{ 
    // Process the single tap here 
    NSLog(@"Scroll view single tapped. touches count : %d", touches.count); 
UITouch *touch = [touches anyObject]; 
    UIImageView *imgView = (UIImageView*)touch.view; 
    NSLog(@"tag is %@", imgView.tag); 
} 

서브 클래스있는 UIScrollView는 touchesDown 응답자가 호출됩니까하지만 '[터치는 anyobject]'두드려되는있는 UIImageView 없습니다.

나는 모든 UIImageView에서 '태그'를 설정하려고했지만 증가하는 카운터로 scrollview를 추가하고 있는데, 어떤 imageview가 나는지에 관계없이 0이 돌아옵니다.

저는 일반적으로 코코아에 익숙하지 않으며 다른 방법으로이 응답자를 활용하는 방법을 잘 모르겠습니다.

어떤 제안이나 조언도 도움이 될 것입니다.

미리 감사드립니다.

답변

12

UIImageView에서 userInteractionEnabled = YES를 설정 했습니까? 기본적으로 해제되어 있습니다.

+0

와우 !! 제이슨, 고마워. 나는 이것에 대해 전혀 몰랐다. 그건 도움이 !!!. 코드에서 'n'uiimageviewers를 만들었습니다. 이 표준이 코코아의 모든 통제에 있습니까? –

+0

아니요, 일반적으로 켜져 있습니다. 일반적으로 상호 작용이없는보기 중 몇 가지가 해제되어 있습니다. UILabel, UIImageView는 마음에 떠오르는 두 가지입니다. –

+1

누군가가 UIGestureRecognizer를 UIView에 추가하면 userInterationEnabled가 자동으로 ON으로 바뀌는 것을 알 수 있습니다.하지만 Apple의 아무도 내 의견을 묻지 않습니다. :-) –

관련 문제