UISwipeGestureRecognizer的一些问题

今天做一个页面的  版本更新的导航显示页


层次是这样,最底下一个UIScrollView,   然后在UIScrollView  上放  uiimageView


需求是滑动到最后一页,继续往右滑动的话  就退出导航状态


用的是UISwipeGestureRecognizer  滑动状态


设置的函数一直没有调用  


后面搜了下   感觉是跟UIScrollView  的滑动冲突了 


加上代理方法


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES; // Recognizers of this class are the first priority

}


问题就解决了   


目前理解来说   是两个空间冲突了   希望有人遇到这种情况吗   或者说出自己的看法



附上代码


imageArray为一个数组,

        "http://data.and.com.cn/image/index/1.png",

        "data.and.com.cn/image/index/2.png",

        "data.and.com.cn/image/index/3.png"



-(void)setImageArray:(NSArray *)imageArray{

    _imageArray = imageArray;

    

    //设置引导视图的scrollview

    

    UIScrollView *  guidePageView= [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.guidePageView = guidePageView;

    guidePageView.userInteractionEnabled = YES;

    guidePageView.contentSize = CGSizeMake(ScreenWidth*imageArray.count, ScreenHeight);

    guidePageView.bounces = NO;

    guidePageView.pagingEnabled = YES;

    guidePageView.showsHorizontalScrollIndicator = NO;

    self.guidePageView.delegate = self;

    [self.view addSubview:guidePageView];

    

    //添加在引导视图上的多张引导图片

    for (int i=0; i<imageArray.count; i++) {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, ScreenHeight)];

//        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageArray[i]]]; //得到图像数据

        //        UIImage *image = [UIImage imageWithData:imgData];

        //        imageView.image = image;

        NSString * urlStr = imageArray[i];

        if ([urlStr rangeOfString:@"http"].location == NSNotFound) {

            NSString * lastStr = [NSString stringWithFormat:@"http://%@", urlStr];

            [imageView sd_setImageWithURL:[NSURL URLWithString:lastStr] placeholderImage:[UIImage imageNamed:@"placehodle"]];

        }else{

            [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"placehodle"]];

        }

        [self.guidePageView addSubview:imageView];

        

        if (i == imageArray.count - 1) {

            imageView.userInteractionEnabled = YES;

            UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];

            swipeGR.numberOfTapsRequired = 1;

//            swipeGR.numberOfTouchesRequired =1;

//            //设置轻扫动作的方向

            swipeGR.delegate = self;

            swipeGR.direction = UISwipeGestureRecognizerDirectionLeft;  //UISwipeGestureRecognizerDirectionLeft

            [imageView addGestureRecognizer:swipeGR];

            

//            UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];

//            [imageView addGestureRecognizer:panGesture];

        }

    }

    

    if (imageArray.count > 1) {

        //设置引导页上的页面控制器

        UIPageControl * imagePageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight - 70, ScreenWidth, 50)];

        self.imagePageControl = imagePageControl;

        imagePageControl.currentPage = 0;

        imagePageControl.numberOfPages = imageArray.count;

        imagePageControl.pageIndicatorTintColor = [UIColor lightGrayColor];

        imagePageControl.currentPageIndicatorTintColor = [UIColor redColor];

        [self.view addSubview:self.imagePageControl];


    }

    

    UIButton * btn = [[UIButton alloc] init];

    btn.frame = CGRectMake(ScreenWidth - 65, 20, 55, 35);

    [btn setBackgroundImage:[UIImage imageNamed:@"tiaoguoicon"] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(swipe) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES; // Recognizers of this class are the first priority

}


//最后一张图片的手势识别

-(void)swipe{

    if ([self.delegate respondsToSelector:@selector(toRootVC)]) {

        [self.delegate toRootVC];

    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview {

    int page = scrollview.contentOffset.x / scrollview.frame.size.width;

    [self.imagePageControl setCurrentPage:page];

}






UIScrollView 上添加手势识别器时,有可能会出现手势冲突的情况。这通常是由于 UIScrollView 自身的手势识别器和添加的手势识别器之间的冲突引起的。 要解决这个问题,有几种可能的方法: 1. 禁用 UIScrollView 的手势识别器。这可以通过将 UIScrollView 的属性 `canCancelContentTouches` 和 `delaysContentTouches` 设置为 `NO` 来实现。但是,这样会使 UIScrollView 的滚动功能也被禁用,因此在某些情况下可能并不可取。 2. 使用手势识别器代理方法来控制手势冲突。在手势识别器代理方法 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 中,可以根据需要返回 `YES` 或 `NO`,以允许或禁止多个手势识别器同时识别手势。 3. 将手势识别器添加到 UIScrollView 的子视图中,而不是直接添加到 UIScrollView 上。这样,手势识别器就不会与 UIScrollView 的手势识别器冲突了。 下面是一个示例代码,演示了如何在 UIScrollView 上添加一个 UISwipeGestureRecognizer 手势识别器,并处理手势冲突的情况: ```objc // 创建一个 UISwipeGestureRecognizer 手势识别器 UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // 设置手势方向 swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; // 将手势识别器添加到 UIScrollView 上 [scrollView addGestureRecognizer:swipeGestureRecognizer]; // 实现手势识别器代理方法,处理手势冲突 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) { return NO; // 禁止 UISwipeGestureRecognizerUIScrollView 同时识别手势 } return YES; } // 处理 UISwipeGestureRecognizer 手势 - (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { // 处理手势事件 } ``` 希望这些提示可以帮助你解决 UIScrollView 上的手势冲突问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值