2012-11-26 3 views
4

UIImageViews으로 채워진 가로 스크롤보기가 있습니다.UIScrollView 내의 UIImageView 탭 감지

UIImageView의 탭을 감지하고 배경색을 변경하고 싶습니다.

아무 래도 탭 제스처가 작동하지 않습니다.

그러나 스크롤보기에 탭 제스처를 추가하면 작동합니다. scrollview.background color은 변경할 수 있습니다.

하지만 포함 된 UIImageViews의 탭을 감지하고 싶습니다.

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)]; 
[scrollView setScrollEnabled:YES]; 
scrollView.backgroundColor = [UIColor orangeColor]; 
[scrollView setShowsHorizontalScrollIndicator:NO]; 
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)]; 
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height); 

for (int aantal=0; aantal < 6; aantal++) { 
    UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)]; 
    item.backgroundColor = [UIColor yellowColor]; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)]; 
    tap.numberOfTapsRequired = 1; 
    tap.cancelsTouchesInView=YES; 
    item.userInteractionEnabled = YES; 
    [item addGestureRecognizer:tap]; 
    [contentOfScrollView addSubview:item]; 
} 

//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
//[scrollView addGestureRecognizer:tap]; 
scrollView.userInteractionEnabled=YES; 
scrollView.delaysContentTouches=NO; 
[scrollView addSubview:contentOfScrollView]; 
[self.view addSubview:scrollView]; 

그리고 이것은 imageTapped 함수입니다.

-(void)imageTapped:(UITapGestureRecognizer *)gesture 
{ 
    NSLog(@"tapped!"); 
    gesture.view.backgroundColor = [UIColor whiteColor]; 
} 
+0

은 그 이후에 코드가 사라지기 때문에 <를 (보다 작음)으로 대체했습니다. – Pip

+0

UITapGestureRecognizer의 대상은 UIImageView 자체가 아니어야합니다. –

답변

12

사용자 상호 작용은 UIImageView 기본적으로 NO로 설정, 그래서 당신은 YES로 설정해야한다. "항목"은 "예"로 설정하고 contentOfScrollView은 설정하지 마십시오.

+0

이렇게 들리는군요. 하지만 이제는 - [UIImageView imageTapped :] : 인식 할 수없는 선택자를 인스턴스로 보냈습니다 – Pip

+0

오 뭔가 시도해 보려고 initWithTarget : 항목을 시도했지만 직접 설정해야합니다. 이제 작동합니다. 고맙습니다! – Pip

+0

통찰력을 가져 주셔서 감사합니다! 필자는 사용자 상호 작용 기능에 대해 읽었으며 스크롤 뷰의 수준뿐만 아니라 이미지 뷰 수준에서도 완벽하게 적용했다고 생각했습니다. 그러나 뷰 아래와 스크롤 뷰 위에있는 중간 레이어를 완전히 간과했습니다. – Pip

1

귀하의 오류가 여기에 있습니다 :

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)]; 

당신은 다음 충돌하지 않습니다, 대신 "항목"의 "자기"로 대상을 변경해야합니다.

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; 
+0

감사합니다. 그러나, 당신이 볼 수 있던대로, 나는 이미 이것 자신을 발견했다. – Pip