简体   繁体   中英

NO gesture recognizer uiimageview in ui scrollview

I want to create a slider of UIImage, with possibility of zoom in the image and swipe to an other image. My swipe is not recognized, and i don't know why. I use cocoa pods, if you know a pod who do this, please, tell me. Or, is it a possibility to use the code of the iphone, in photo gallery?

This is my code :

- (void)viewDidLoad
{


    [super viewDidLoad];
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;


    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [imgView setUserInteractionEnabled:YES];

    [imgView addGestureRecognizer:swipeLeft];
    [imgView addGestureRecognizer:swipeRight];


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"********************/test.json" parameters:nil success:^(AFHTTPRequestOperation *operation, NSArray *responseObject) {
        //NSLog(@"URL");
        //NSLog(@"JSON: %@", responseObject);


        urlArray = responseObject;

        //NSLog(@" %@", urlArray);
        int i=0;

        url = [NSURL URLWithString:[urlArray objectAtIndex:i]];

        //NSLog(@"%@", url);
        data = [NSData dataWithContentsOfURL:url];

        img = [[UIImage alloc] initWithData:data];



    [self.view setBackgroundColor:RGB(32,32,32)];

    if(orientation == UIInterfaceOrientationPortrait) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:scrollView];
        self.scrollView = scrollView;


    if (img.size.width>img.size.height){
    //format d'images paysage
        NSLog(@"PORTAIT image paysage");

        NSLog(@"%f", img.size.width);
        NSLog(@"%f", img.size.height);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width+img.size.height, self.scrollView.frame.size.height)];
        //imgView.contentMode = UIViewContentModeScaleAspectFit;
       imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.scrollView addSubview:imageView];
        self.scrollView.contentSize = imageView.frame.size;
        self.imgView = imageView;
        [self.imgView setImage:img];


    }

    if (img.size.height>img.size.width && orientation == UIInterfaceOrientationPortrait){
        //format d'images portrait
        NSLog(@"PORTAIT image portrait");
        NSLog(@"%f", img.size.width);
        NSLog(@"%f", img.size.height);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,  img.size.height-img.size.width, self.scrollView.frame.size.height)];
        //imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.scrollView addSubview:imageView];
        self.scrollView.contentSize = imageView.frame.size;
        self.imgView = imageView;

    }


        if (img.size.height==img.size.width){
            //format d'images portrait
            NSLog(@"ICI %f", img.size.width);
            NSLog(@"%f", img.size.height);
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, self.scrollView.frame.size.height)];
            //imgView.contentMode = UIViewContentModeScaleAspectFit;
            imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            [self.scrollView addSubview:imageView];
            self.scrollView.contentSize = imageView.frame.size;
            self.imgView = imageView;

            [self.imgView setImage:img];

        }

    }


    } failure:nil];
}


- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");
    }

}

The UISwipeGestureRecognizer s are not being triggered because the UIImageView you have attached them to is inside of a UIScrollView . The UIScrollView will be capturing the gestures before the UIImageView gets them.

The best way to do what you are proposing is to put the UIImageView inside of it's own UIScrollView for zooming, like you have done, and then put each of the UIScrollView 's in a larger UIScrollView with pagingEnabled . Obviously you will have to do some basic frame calculation to position the UIScrollView 's inside of the top UIScrollView and set it's contentSize.

You could always use a UIPageViewController which is probably what I would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM