ui scrollview 中没有手势识别器 uiimageview

Posted

技术标签:

【中文标题】ui scrollview 中没有手势识别器 uiimageview【英文标题】:NO gesture recognizer uiimageview in ui scrollview 【发布时间】:2014-05-15 08:40:16 【问题描述】:

我想创建一个 UIImage 滑块,可以放大图像并滑动到其他图像。 我的滑动无法识别,我不知道为什么。 我用可可豆荚,如果你知道这样做的豆荚,请告诉我。 或者,是否可以在照片库中使用 iphone 的代码?

这是我的代码:

- (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");
    


【问题讨论】:

【参考方案1】:

UISwipeGestureRecognizers 没有被触发,因为您将它们附加到的 UIImageView 位于 UIScrollView 内部。 UIScrollView 将在 UIImageView 获取手势之前捕获手势。

按照您的建议进行操作的最佳方法是将UIImageView 放入它自己的UIScrollView 中以进行缩放,就像您所做的那样,然后将每个UIScrollView 放入更大的@987654327 @ 和pagingEnabled。显然,您必须进行一些基本的帧计算才能将UIScrollView 定位在顶部UIScrollView 内部并设置它的contentSize。

你总是可以使用UIPageViewController,这可能是我会做的。

【讨论】:

非常感谢。是否可以将 UiPageViewController 与 zoom 一起使用?我尝试将我的scrollView1放在另一个scrollViewTop中,并在scrollViewTop上添加滑动手势,但它不起作用...... 不要添加滑动手势...这是一个滚动视图。它会滚动。

以上是关于ui scrollview 中没有手势识别器 uiimageview的主要内容,如果未能解决你的问题,请参考以下文章

在使用手势识别器添加子视图时遇到 UI 故障问题

ScrollView - 手势识别器 - 垂直滑动

无法识别滑动手势

如何在 Xcode UI 测试中识别控制中心元素?

OC ScrollView里面含有拖动的TableView 如何响应拖动的手势

UI 平移手势不起作用