Brittany
Brittany

Reputation: 1439

Tap imageView to view Fullscreen image

I have a uiimageview in my detail view, and I want to make it so that when I tap on the image, it opens the image full screen. However, my code below doesn't seem to be working. Any idea what I seem to be doing wrong?

FullViewController.h

@interface FullArticleViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate>


    {
        IBOutlet UITapGestureRecognizer *tap;
        IBOutlet UIScrollView *scroller;
        IBOutlet UILabel *firstnameLabel;
        IBOutlet UILabel *bodyLabel;
        IBOutlet UILabel *descriptionLabel;


        BOOL isFullScreen;
        CGRect prevFrame;        
    }

FullViewController.m

   - (void)viewDidLoad
    {

        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.

           if ([[articleDetail objectForKey:@"ytID"] isEqualToString:@"none"]) {

           featureImage.userInteractionEnabled = YES;

           NSString *imageUrl = [NSString stringWithFormat:@"image/%@",cellImageLink];

           [self.featureImage sd_setImageWithURL:[NSURL URLWithString:imageUrl]];

           [scroller setScrollEnabled:YES];

           featureImage.userInteractionEnabled = YES;

           [scroller bringSubviewToFront:featureImage];

        UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
        [tapgesture setNumberOfTapsRequired:1];

        [featureImage addGestureRecognizer:tap];

    }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
    {
        BOOL shouldReceiveTouch = YES;

        if (gestureRecognizer == tap) {
            shouldReceiveTouch = (touch.view == featureImage);
        }
        return shouldReceiveTouch;
    }

    -(void)imgToFullScreen{
        if (!isFullScreen) {
            [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
                //save previous frame
                prevFrame = featureImage.frame;
                [featureImage setFrame:[[UIScreen mainScreen] bounds]];
            }completion:^(BOOL finished){
                isFullScreen = true;
            }];
            return;
        } else {
            [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
                [featureImage setFrame:prevFrame];
            }completion:^(BOOL finished){
                isFullScreen = false;
            }];
            return;
        }
    }

Edit: See code updated above, and hierarchy below

enter image description here

Upvotes: 1

Views: 830

Answers (1)

freshking
freshking

Reputation: 1864

Try this in your viewDidLoad: method:

[featureImage setUserInteractionEnables:YES];

UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
[tapgesture setNumberOfTapsRequired:1];
[featureImage addGestureRecognizer:tapgesture];

Upvotes: 1

Related Questions