![](/img/trans.png)
[英]Unable to detect tap in UIScrollView inside another UIScrollView
[英]Detect tap on UIImageView inside UIScrollView
我有一個填充UIImageViews
的水平滾動視圖。
我想檢測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];
}
對於UIImageView
,默認情況下用戶交互設置為NO
,因此您需要將其設置為YES
。 您為“item”設置為yes,但不為contentOfScrollView
設置為yes。
你的錯誤在這里:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:@selector(imageTapped:)];
您需要將目標更改為“self”而不是“item”,然后它不會崩潰。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.