gpt4 book ai didi

ios - 检测在 UIScrollView 中点击 UIImageView

转载 作者:可可西里 更新时间:2023-11-01 04:11:31 26 4
gpt4 key购买 nike

我有一个水平 ScrollView ,其中填充了 UIImageViews

我想检测 UIImageView 上的点击并更改其背景颜色。

不知何故,点击手势无法正常工作。

但是,当我向 ScrollView 添加轻击手势时,它起作用了。 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 则不是。

关于ios - 检测在 UIScrollView 中点击 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com