如何获取uiscrollview的uiimage子视图索引

时间:2013-11-23 07:38:34

标签: ios iphone ios5 ios6 uiscrollview

我需要检测放在scrollview中的uiimage的所选图像。

我的代码在这里:

int newscrollviewY = 40;
    for(int i = 0; i<1; i++)
    {
        scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(5, newscrollviewY, 310, 100)];
        scrollView.showsVerticalScrollIndicator = YES;
        scrollView.scrollEnabled = YES;
        scrollView.userInteractionEnabled = YES;
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.delegate=self;
        int imgx = 1;

        for(int img=0; img<[images count]; img++)
        {

            UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
            imageView1.backgroundColor = [UIColor whiteColor];
            imageView1.image = [images objectAtIndex:img];
            imageView1.contentMode = UIViewContentModeScaleAspectFit;
            [imageView1 setNeedsDisplay];
            [imageView1 setUserInteractionEnabled:YES];
            [imageView1 setMultipleTouchEnabled:YES];
            [scrollView addSubview:imageView1];


            imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            imageButton.frame = CGRectMake(imgx,0,100,100);
            [imageButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
            imageButton.tag = img;
            [scrollView addSubview:imageButton];
            imgx = imgx + 103;


        }

请使用imageButton或其他内容帮助检测所选图像。

5 个答案:

答案 0 :(得分:0)

他们设置了“按钮”的标签,这已经是图像阵列中图像的编号。所以你需要做的就是从'按钮'获取标签。

确保按钮单击功能从您收到的发件人(ID)中检索标记。

例如有点像这样:

- (IBAction)buttonClicked:(id)sender {
    UIButton *button = (UIButton *)sender;
    UIImage selectedImages = [images objectForKey: [button tag]];
}

答案 1 :(得分:0)

buttonClicked:替换您的选择器 请确保您在选择器方法之后添加“”。请关注或替换以下代码:

[imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

然后在.m文件中添加选择器定义

- (void) buttonClicked: (id) sender
{
    // here you can get the tag of button.

    NSInteger tag = ((UIButton *)sender).tag;

    // do your implementation after this
}

希望这能解决您的问题。享受编码.. !!

答案 2 :(得分:0)

如果您使用的是UIImageview,那么您必须使用与{Button}相同的UITapGestureRecognizer。你只需要添加UITapGestureRecognizer,如: -

    for(int img=0; img<[images count]; img++)
    {

        UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(imgx,0,100,100)];
        imageView1.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)];
        frameTapGesture.numberOfTapsRequired=1;

        imageView1.image = [images objectAtIndex:img];
        imageView1.contentMode = UIViewContentModeScaleAspectFit;
        [imageView1 setNeedsDisplay];
        [imageView1 setUserInteractionEnabled:YES];
        [imageView1 setMultipleTouchEnabled:YES];
         imageView1.tag=img;
        [_imgView addGestureRecognizer:frameTapGesture];
        [scrollView addSubview:imageView1];

    }.

- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
    UIView *view = sender.view; //cast pointer to the derived class if needed
    NSLog(@"%d", view.tag);
}

减少代码无需添加具有ImageView相同框架的Button用于获取Click事件,或者您也可以设置UIButton的图像,以便您想要使用UIBUtton然后删除UIImageview Code else您只需使用UITapGestureRecognizer即可使用- (void)frameTapGesture:(UITapGestureRecognizer*)sender

获取点击事件

答案 3 :(得分:0)

请尝试此代码

for(int img=0; img<[images count]; img++)
    {
        imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [imageButton setImage:[UIImage imageNamed:[images objectAtIndex:i]] forState:UIControlStateNormal];
        imageButton.frame = CGRectMake(imgx,0,100,100);
        [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        imageButton.tag = img;
        [scrollView addSubview:imageButton];
        imgx = imgx + 103;


    }
- (void) buttonClicked: (id) sender
{
   UIButton *button = (UIButton *)sender;
   UIImage selectedImage = button.currentImage;
}

答案 4 :(得分:0)

设置按钮目标

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

//获取按钮操作

-(void) buttonAction:(UIButton *)sender
{
 NSLog(@"%dl",sender.tag);
// do your implementation after this
}