-2

I have used third party lib to show array of images in a slider . The images in an array comes from server, now i want to show the image in a bigger image view on a tap gesture. When anyone of the image user tap it should be shown in big imageview. I have tried some code but it is not working. My code is,

  int i=0;

    tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tap.numberOfTapsRequired = 1;
    [_ImageView setUserInteractionEnabled:YES];
    [_ImageView addGestureRecognizer:tap];
    tap = [_imagesData objectAtIndex:i];
    NSLog(@"TTT %@",tap);

    [self.view addSubview:_fullImage];

The method is ,

-(void)expandImage:(UITapGestureRecognizer*)recogniser
{
    [self.view addSubview:_fullImage];

    UIButton *closeButton = [[UIButton alloc]init];
    [closeButton setTitle:@"Close" forState:UIControlStateNormal];
   // [closeButton addTarget:self action:@selector(Closetab) forControlEvents:UIControlEventTouchUpInside];
    [_fullImage addSubview:closeButton];

    UIImageView *photoView = [[UIImageView alloc]init];
    [photoView setBackgroundColor:[UIColor blueColor]];
    [_fullImage addSubview:photoView];
   // photoView.accessibilityIdentifier = @"nature2.png";

    [_fullImage addSubview:_fullImage];

}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
Oneeb
  • 75
  • 1
  • 1
  • 9
  • see this https://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views – Anbu.Karthik Sep 25 '17 at 11:42
  • let me check. @Anbu.Karthik – Oneeb Sep 25 '17 at 11:46
  • it is not tapping and showing me the tapped imaged in an imagview. @Anbu.Karthik – Oneeb Sep 25 '17 at 11:48
  • I think no need of this in inside the `-(void)expandImage:(UITapGestureRecognizer*)recogniser` , hide this `[self.view addSubview:_fullImage];` and this `[_fullImage addSubview:_fullImage];` and try once – Anbu.Karthik Sep 25 '17 at 11:51
  • and you forget to set the frame of your close button and photoView – Anbu.Karthik Sep 25 '17 at 11:52
  • can you show the code related to this `_fullImage` – Anbu.Karthik Sep 25 '17 at 11:55
  • actually , i have tried this code from net. @Anbu.Karthik – Oneeb Sep 25 '17 at 12:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155242/discussion-between-anbu-karthik-and-oneeb). – Anbu.Karthik Sep 25 '17 at 12:07
  • i have used uiview name fullImage and on that view used UIImage View name fulImageView. @Anbu.Karthik – Oneeb Sep 25 '17 at 12:08
  • you tried in wrong way, its add close button on every time on tapped – Anbu.Karthik Sep 25 '17 at 12:10
  • Slider? What does that mean? A UISlider? Are you saying that you have a small image on-screen, and you want to add a larger image on top of the smaller when the user taps? Finally, what do you mean "I have tried some code but it is not working"? Not working how? What does it do and how do that not meet your expectations? – Duncan C Sep 28 '17 at 02:16

1 Answers1

0

here I attached the sample project , customize yourself

step1

create the one full screen view for preview the image (use one UIView, one close btn, one imageview)

@interface ViewController () <UIScrollViewDelegate, TAPageControlDelegate>
{
NSTimer *timer;
NSInteger index;
//
IBOutlet UIImageView *imgfull;
IBOutlet UIView *fullimagevie;
IBOutlet UIButton *btnClose;
}

step2

add the gesture for tap the image event and set the frame of your duplicate view as bottom of viewcontroller

- (void)viewDidLoad
{
[super viewDidLoad];

 [self setviewframe:self.view.frame.size.height + 10];
self.imagesData = @[@"image1.jpg", @"image2.jpg", @"image3.jpg"];
for(int i=0; i<self.imagesData.count;i++){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tap.numberOfTapsRequired = 1;
    tap.view.tag = i;
    [imageView setUserInteractionEnabled:YES];
    [imageView addGestureRecognizer:tap];

    [self.scrollView addSubview:imageView];
}

step3

handle the image tap for preview the image

-(void)expandImage:(UITapGestureRecognizer*)recogniser
 {
imgfull.image = [UIImage imageNamed:[self.imagesData objectAtIndex:recogniser.view.tag]];
[UIView transitionWithView:fullimagevie
                  duration:0.4
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                   [self setviewframe:0];
                }
                completion:NULL];

}

step4

dismiss the bigger image view on tap the close button

- (IBAction)btnClose:(UIButton *)sender {

[UIView transitionWithView:fullimagevie
                  duration:0.4
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    [self setviewframe:self.view.frame.size.height + 10];
                }
                completion:NULL];

}

step5

create the common method for set the frame for your bigger imageview

-(void)setviewframe:(CGFloat)coordinate{
CGRect modifyframe = fullimagevie.frame;
modifyframe.origin.y = coordinate;
fullimagevie.frame = modifyframe;
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143