Ayubxon Ubaydullayev
Ayubxon Ubaydullayev

Reputation: 349

I want to know which image is pressed in UIScrollView in objective c

I have UIScrollView that consists of UIImageViews. When I touch any imageView, I want to display this UIImageView's image in other window. how can I do that? any ideas .

I use UITapGestureRecognizer but I cant find which imageView is pressed.

this is my code:

for (NSArray *photo in self.photos) {
        imageName = [photo valueForKey:@"filename"];
        imageName = [imageName stringByAppendingString:@".jpg"];
        UIImage *image = [UIImage imageNamed:imageName];

        UIImageView *imageView;
        imageView = [[UIImageView alloc] initWithImage:image];
        [imageView setFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];

        imageX += imageSize + 10;
        if (imageX + imageSize >= self.view.bounds.size.width){
            imageX = 15;
            imageY += imageSize + 10;
        }

        imageView.userInteractionEnabled = YES;
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goFullPhotoGallery:)];
        [imageView addGestureRecognizer:tap];
        [scrollView addSubview:imageView];
    }

Upvotes: 1

Views: 129

Answers (3)

Viper
Viper

Reputation: 1408

Try this -

for (NSArray *photo in self.photos) {
        imageName = [photo valueForKey:@"filename"];
        imageName = [imageName stringByAppendingString:@".jpg"];
        UIImage *image = [UIImage imageNamed:imageName];
UIButton *starBtn  =   [[UIButton alloc]initWithFrame:CGRectMake(imageX, imageY, imageSize, imageSize)];
        [starBtn setBackgroundColor:[UIColor clearColor]];
        [starBtn setImage:image forState:UIControlStateNormal];
        [starBtn setTag:i+1];
        [starBtn setTitle:imageName forState:UIControlStateNormal];
        [starBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [starBtn addTarget:self action:@selector(goFullPhotoGallery:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:starBtn];
}

and goFullPhotoGallery function like this

-(void)goFullPhotoGallery:(UIButton *)imgView{
    NSString *imgName = imgView.titleLabel.text;
}

Upvotes: 0

Cyrille
Cyrille

Reputation: 25144

A better solution would be to use a UITableView or a UICollectionView, and listen to cell selection events...

Upvotes: 3

for this type of task i generally use a UIButton over image view and addTarget to them and get image with button tag.

Upvotes: 3

Related Questions