UISwipeGestureRecognizer的一些问题

来源:互联网 发布:五十知天命六十是什么 编辑:程序博客网 时间:2024/06/03 20:15

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


层次是这样,最底下一个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];

}






0 0
原创粉丝点击