【问题标题】:UISwipeGestureRecognizer inside UIScrollView does not swipe right - Objective CUIScrollView 内的 UISwipeGestureRecognizer 不会向右滑动 - Objective C
【发布时间】:2017-10-25 06:08:16
【问题描述】:

我在滚动视图中使用了两个滑动手势,因此我可以更改照片和放大,向左滑动正常且流畅,而向右滑动无法正常工作,多次滑动后它可能会触发一次,我正在使用这段代码:

//image zoom
    self.scrollView.minimumZoomScale=1.0;
    self.scrollView.maximumZoomScale=6.0;
    self.scrollView.contentSize=CGSizeMake(1280, 960);
    self.scrollView.delegate=self;
    self.scrollView.clipsToBounds = YES;
    self.scrollView.scrollEnabled = false;

    UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
    swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeleft];

    UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
    swiperight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swiperight];

以及这两种处理滑动的方法

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    NSLog(@"LEFTtttttt");
    if(index > 0 && index < _photoCount){
        index = index - 1;
        [self loadImage];
    }

}

-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
       NSLog(@"RIGHTttttt");
    if(index >= 0 && index < _photoCount - 1){
        index = index + 1;
        [self loadImage];
    }
}

我当然搜索了一个解决方案并尝试了不同的代码但它不起作用,我添加了

[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:swiperight];

还有这个,

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if (scrollView.zoomScale!=1.0) {
        // Zooming, enable scrolling
        scrollView.scrollEnabled = TRUE;
    } else {
        // Not zoomed, disable scrolling so gestures get used instead
        scrollView.scrollEnabled = FALSE;
    }
}

唯一的区别是当我添加后一个代码时,当我放大时向右滑动正常工作,而它应该在相反的情况下工作。

我是否遗漏了代码中的某些内容?

谢谢。

【问题讨论】:

    标签: ios objective-c uiscrollview uigesturerecognizer


    【解决方案1】:

    请将滑动手势添加到不在 self.view 上的 scrollView 以使其在用户在 scrollView 上滑动时正常工作。如下所示

    UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
    swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.scrollView addGestureRecognizer:swipeleft];
    
    UISwipeGestureRecognizer * swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
    swiperight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.scrollView addGestureRecognizer:swiperight];
    

    这可能是因为滚动视图在 self.view 上方呈现。

    【讨论】:

    • 如果你还有问题分享我会帮你的
    • 忘了添加,我试过这个解决方案,但没有用。
    • @yasserh 您现在面临的具体问题是什么?请分享,
    • 同样的问题,可以向左滑动但不能向右滑动,除非我放大。
    • 添加这行后能不能试试[swipeRight requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多