次のコードを使用して、約 29 個の画像を に追加しUIScrollView
ます。
- (void)setupHorizontalScrollView
{
_scrollViewEffects.delegate = self;
[_scrollViewEffects setCanCancelContentTouches:NO];
_scrollViewEffects.clipsToBounds = NO;
_scrollViewEffects.scrollEnabled = YES;
_scrollViewEffects.pagingEnabled = YES;
NSUInteger nimages = 0;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:@"e%d.png", nimages];
UIImage *imageThumb = [UIImage imageNamed:imageName];
[imageView setTag:nimages];
if (tot==29) {
break;
}
if (4==nimages) {
nimages=0;
}
imageView = [[UIImageView alloc] initWithImage:imageThumb];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleTap];
CGRect rect = imageView.frame;
rect.size.height = 50;
rect.size.width = 50;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 1;
imageView.frame = rect;
[_scrollViewEffects addSubview:imageView];
cx += imageView.frame.size.width+5;
tot++;
}
//self.pageControl.numberOfPages = nimages;
[_scrollViewEffects setContentSize:CGSizeMake(cx, [_scrollViewEffects bounds].size.height)];
[imageView setUserInteractionEnabled:YES];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
NSLog(@"image tapped!!!");
}
ご覧のとおり、UserInteraction は有効で、すべてが機能しますが、コードは UIScrollView からの最後の画像のタップのみを検出します。なにが問題ですか?