1
votes

I want to touch on UIImageView and that image display in another view in full size using navigate the view. same as gallery in mobile. just creating demo programmatically. because I'm new in iphone developing.

Here is my code.

- (void)viewDidLoad
{
    NSLog(@"Enter in viewDidLoad method");

    UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);

    [self.view setUserInteractionEnabled:YES];
    [scroll setUserInteractionEnabled:YES];
    [self.view addSubview:scroll];

    int x=5;
    int y=5;
    int hei=100;
    int wid=100;
    int k=1;
    for (int i=0; i<3; i++) 
    {
        for(int j=0;j<50;j++)
        {
            imageView = [[UIImageView alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
            [imageView setImage:[UIImage imageNamed:@"Motocross.png"]];
            [imageView setUserInteractionEnabled:YES];
            scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
            [imageView setTag:k];
            [scroll addSubview:imageView];                        

            NSLog(@"The tag is == %d",k++);
            NSLog(@" (%d,%d)x == %d",i,j,(x+wid)*i+5);
            NSLog(@" (%d,%d)y == %d",i,j,(y+hei)*j+5);

        } 

    }

    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    NSLog(@"Enter in touchesBeganWithEvent method");
    UITouch *touch = [touches anyObject];
    if ([touch view] == imageView)
    {
        NSLog(@"Enter in if condition of touch");
        DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
        detailViewController.imageD = imageView.image;

        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];

        NSLog(@"Exit from if condition of touch");
    }
    NSLog(@"Exit from touchesBeganWithEvent method");
}

I want to tap on imageView and view will navigate to detailViewController and display that image in big size. but this touchesBegan method not called when i tap on imageView. In this i'm not creating array of UIImageView just alloc init in loop.. Thank you!

3
is the touchesBegan method called? did you try inserting a breakpoint inside the method? - vin
@i suppose your checking it incorrectly try this: if([touch.view isKindOfClass:[UIImageView class]]) - vin

3 Answers

2
votes

Rather than using touchesBegan: on the view, consider adding a UITapGestureRecognizer to each image view. When the image view is tapped (you are setting userInteractionEnabled already which is good) then you can get the view that was tapped with sender.view.

Note that you create a new instance of UITapGestureRecognizer for each image view that you have, but that the target/action of each gesture can be the same.


The problem with your current code is that this check:

if ([touch view] == imageView)

verifies the touch only against the last image view that you added to the scroll view. So, touching the last one should work, but all the others won't.

As per the comment from @vin, the correct solution to that issue is to verify the class type of the view that was touched:

if ([[touch view] isKindOfClass:[UIImageView class]]) {

(though the scroll view may have other image view subclasses than just the ones you add...)

0
votes

Try bellow code or its better to use Collection view

    - (void)viewDidLoad
{
    NSLog(@"Enter in viewDidLoad method");

    UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);

    [self.view setUserInteractionEnabled:YES];
    [scroll setUserInteractionEnabled:YES];
    [self.view addSubview:scroll];

    int x=5;
    int y=5;
    int hei=100;
    int wid=100;
    int k=1;
    for (int i=0; i<3; i++)
    {
        for(int j=0;j<50;j++)
        {
            UIButton *buttonForImage=[[UIButton alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
            [buttonForImage setImage:[UIImage imageNamed:@"Motocross.png"] forState:UIControlStateNormal];
            [buttonForImage addTarget:self action:@selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
            [buttonForImage setTag:k];
            [scroll addSubview:buttonForImage];

            NSLog(@"The tag is == %d",k++);
            NSLog(@" (%d,%d)x == %d",i,j,(x+wid)*i+5);
            NSLog(@" (%d,%d)y == %d",i,j,(y+hei)*j+5);

        }

    }

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)imageButtonPressed:(UIButton*)sender{

    NSLog(@"Enter in if condition of touch");
    DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.imageD = sender.imageView.image;

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

}
0
votes

You need to use UICollectionview rather than trying to juggle with rows /colns, images touches etc... Good tutorial is here... UICollectionView Tutorial (Assuming you dont support below iOS 6.0)