2012-08-01 4 views
1

나는 썸네일 이미지 그리드를 가지고 있는데, 그 중 하나가 터치되면 전체 이미지를 보여주고 싶습니다. 이제 UIImageView가 터치 이벤트에 응답하지 않지만 this answer에 제안 된대로 이벤트를 처리 할 UIButton을 만들었습니다. 아래 코드 참조 :이미지보기가 터치 이벤트에 응답하지 않습니다

- (void)displayImage 
{ 
     NSUInteger i = 0; // The actual code is different and works; this is just for brevity's sake. 

     // UILazyImageView is a subclass of UIImageView 
     UILazyImageView *imageView = [[UILazyImageView alloc] initWithURL:[NSURL URLWithString:[[[self images] objectAtIndex:i] thumbnailUrl]]]; 

     UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [imageButton setFrame:[imageView frame]]; 
     [imageButton addTarget:self action:@selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside]; 
     [imageView addSubview:imageButton]; 

     [[self containerView] addSubview:imageView]; 
     [imageView release]; 
    } 
} 

- (void)imageTapped:(id)sender 
{ 
    NSLog(@"Image tapped."); 

    // Get the index of sender in the images array 
    NSUInteger index = 0; // Don't worry, I know. I'll implement this later 

    FullImageViewController *fullImageViewController = [[[FullImageViewController alloc] initWithImageURL:[NSURL URLWithString:[[[self images] objectAtIndex:index] url]]] autorelease]; 
    [[self navigationController] pushViewController:fullImageViewController animated:YES]; 
} 

확인. 그래서 커스텀 버튼을 만들고, 이미지 프레임과 일치하도록 프레임을 설정하고, 내부적으로 반응하고 응답하는 방법에 대해 말하며 이미지의 서브 뷰에 추가했습니다. 하지만 이걸 실행할 때 나는 아무것도 얻지 못합니다. "이미지가 탭되었습니다." 콘솔에 나타나지 않아서 메시지가 전송되지 않는다는 것을 알고 있습니다. 여기서 내가 뭘 잘못하고 있니?

많은 도움을 주셔서 감사합니다.

답변

3

은 userInteractionEnabled 속성은 당신이 전부 버튼을 제거 할 수 imageView.userInteractionEnabled = YES을 설정하는이 라인 또한

imageView.userInteractionEnabled = YES; 
1

이있는 UIView로부터 상속 한 속성입니다,하지만 애플의 문서에 따라, UIImageView에 모든 이벤트를 무시하고, NO에 대한 기본 값을 변경

imageView.userInteractionEnabled = YES; 

보십시오. 기본 UIImageView에 의한

1

을 추가하고 탭을 처리 할 수있는 UIImageViewUITapGestureRecognizer를 추가 NO

로 설정하고있다 . 모양은 다음과 같습니다.

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
[imageView addGestureRecognizer:recognizer]; 
관련 문제