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];
}
은 그 이후에 코드가 사라지기 때문에 <를 (보다 작음)으로 대체했습니다. – Pip
UITapGestureRecognizer의 대상은 UIImageView 자체가 아니어야합니다. –