Objective c 在UIScrollview中选择特定图像

Objective c 在UIScrollview中选择特定图像,objective-c,uiscrollview,Objective C,Uiscrollview,我有一个scrollview,它将图像添加为子视图。我需要能够选择或点击图像。换句话说,捕获图像的名称。请看下面我的代码 for (j = 1; j <= 12; j++) { NSString *imageName = [NSString stringWithFormat:@"%@",[months objectAtIndex:j-1]]; NSLog(@"imagename is %@",imageName); UIImage *image = [UIImage

我有一个scrollview,它将图像添加为子视图。我需要能够选择或点击图像。换句话说,捕获图像的名称。请看下面我的代码

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];

用于(j=1;j为什么不在scrollView中使用UIButton呢?这样,您就可以使用标准的UIButton方法,不需要使用手势识别器,您就可以看到按钮上被按下的图像。

为什么不在scrollView中使用UIButton呢?这样您就可以使用标准的UIButton了方法,无需使用手势识别器,您就可以看到按下按钮上的图像。

将唯一标记值与每个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
}
以前从未尝试过类似的方法,但它可能会起作用

编辑:我从另一个类似问题的答案中复制粘贴了这段代码
因此,请根据您的要求调整条件。

将唯一的标记值与每个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
}
以前从未尝试过类似的方法,但它可能会起作用

编辑:我从另一个类似问题的答案中复制粘贴了这段代码
因此,请根据您的要求调整条件。

听起来是个好主意,我明天会试试,让您知道听起来是个好主意,我明天会试试,让您知道