我有一个滚动视图,其中添加了作为子视图的图像。我需要能够选择或点击图像。换句话说,捕获被轻拍的图像的名称。请参阅下面的代码。
for (j = 1; j <= 12; j++)
{
NSString *imageName = [NSString stringWithFormat:@"%@",[months objectAtIndex:j-1]];
NSLog(@"imagename is %@",imageName);
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = smallScrollObjHeight;
rect.size.width = smallScrollObjWidth;
imageView.frame = rect;
imageView.tag = j;
[smalltapRecognizer setNumberOfTapsRequired:1];
[smalltapRecognizer setDelegate:self];
[imageView addGestureRecognizer:smalltapRecognizer];
[smalltapRecognizer release];*/
NSLog(@"tag value is %d ",imageView.tag);
[monthscroll addSubview:imageView];
[imageView release];
}
[self layoutsmallScrollImages];
UITapGestureRecognizer *smalltapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smalltapped:)];
[smalltapRecognizer setNumberOfTapsRequired:1];
[smalltapRecognizer setDelegate:self];
[monthscroll addGestureRecognizer:smalltapRecognizer];
[smalltapRecognizer release];
// ----------------------------
- (无效)smalltapped:(ID)发送方 { 的NSLog(@ “抽头”); //需要知道如何从这里开始。 }
我的程序检测到水龙头并在小型功能中打印水龙头日志。我需要知道如何捕捉所选或点击的图像的名称。
答案 0 :(得分:1)
为什么不直接在scrollView中使用UIButtons?这样你可以使用标准的UIButton方法,不需要打扰手势识别器,并且你将能够看到按下按钮的图像。
答案 1 :(得分:0)
将唯一标记值与每个UIImageView实例相关联:
在viewDidLoad中或初始化图像的位置:
myImageView1.tag = 1;
myImageView2.tag = 2;
myImageView3.tag = 3;
然后尝试手势或类似的东西:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
int t = [touch view].tag;
UIImageView *imageView = (UIImageView *) [self.view viewWithTag:t];
//your logic here since you have recognized the imageview on which user touched
}
之前没有尝试过类似的东西,但它可能会起作用。
编辑:我从另一个类似问题的here复制粘贴此代码 所以请根据您的要求调整条件。