簡體   English   中英

如何獲取uiscrollview的uiimage子視圖的索引

[英]How to get index of uiimage subview of uiscrollview

我需要檢測uiimage的選定圖像,該圖像位於scrollview內。

我的代碼在這里:

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或其他方法檢測選定的圖像。

他們以這種方式設置“按鈕”的標簽已是圖像數組中圖像的編號。 因此,您所需要做的就是從“按鈕”中獲取標簽。

確保單擊按鈕的功能從您收到的發件人(id)中檢索標簽。

例如如下所示:

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

將選擇器替換為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
}

希望這能解決您的問題。 享受編碼..!

作為標准,您質疑是否使用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代碼,否則您只需使用UITapGestureRecognizer即可通過- (void)frameTapGesture:(UITapGestureRecognizer*)sender

請嘗試此代碼

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

設置按鈕目標

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

//獲取按鈕動作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM