2015-07-21 63 views
1

我试图在UIScrollView上识别上/下滑动手势。 ScrollView支持分页,每个页面都有一个UIImageView作为ScrollView的子视图。我试图创建UISwipeGestureRecognizers并将它们与ScrollView和子视图UIImageView关联。两者都不起作用。为什么?滑动手势无法在UIScrollView和子视图上工作

我怎样才能可靠地刷上/下工作?我的要求是在ScrollView之上显示横幅(UIView),当用户向上滑动并向上滑动时将其解除。

这是我目前的实施。

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

    NSDictionary *aNews = [NSDictionary dictionaryWithDictionary:[arrNews objectAtIndex:i]]; 

    CGRect frame; 
    frame.origin.x = self.viewScrollView.frame.size.width * i; 
    frame.size = self.viewScrollView.frame.size; 

    self.viewScrollView.pagingEnabled = YES; 

    UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(frame.origin.x, 400, frame.size.width, 0)]; 
    subview.backgroundColor = [UIColor whiteColor]; 
    subview.alpha = 0.9; 
    subview.tag = 999; 

    // background image 
    UIImageView *imgViewBackGround = [[UIImageView alloc]initWithFrame:frame]; 
    imgViewBackGround.image = nil; 
    [imgViewBackGround sd_setImageWithURL:[NSURL URLWithString:[[arrNews objectAtIndex:i] objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"PlaceholderBanner"]]; 

    // news title 
    UILabel *lblNewsTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 230, 0)]; 
    lblNewsTitle.text = [aNews objectForKey:@"title"]; 
    lblNewsTitle.font = [UIFont boldSystemFontOfSize:20]; 
    lblNewsTitle.numberOfLines = 0; 
    lblNewsTitle.lineBreakMode = NSLineBreakByWordWrapping; 
    [lblNewsTitle sizeToFit]; 
    lblNewsTitle.textColor = [UIColor blackColor]; 
    [subview addSubview:lblNewsTitle]; 

    [subview setFrame:CGRectMake(frame.origin.x, 400, frame.size.width, lblNewsTitle.frame.size.height + 30)]; 
    CGFloat screenHieght = [UIScreen mainScreen].bounds.size.height; 
    if(screenHieght>500){ 
     [subview setFrame:CGRectMake(frame.origin.x, self.view.frame.size.height - subview.frame.size.height, frame.size.width, lblNewsTitle.frame.size.height + 30)]; 
    } 
    else{ 
     [subview setFrame:CGRectMake(frame.origin.x, 480 - subview.frame.size.height, frame.size.width, lblNewsTitle.frame.size.height + 30)]; 
    } 

    // up button 
    UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnUp.frame = CGRectMake(277, 10, 30, 30); 
    [btnUp setBackgroundImage:[UIImage imageNamed:@"upArrow"] forState:UIControlStateNormal]; 
    btnUp.backgroundColor = [UIColor clearColor]; 
    btnUp.tag = i; 
    [btnUp addTarget:self action:@selector(btnUpTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [subview addSubview:btnUp]; 


    // Swipe gesture up and down 
    UISwipeGestureRecognizer *swipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; 
    swipeGestureUp.direction = UISwipeGestureRecognizerDirectionUp; 
    [imgViewBackGround addGestureRecognizer:swipeGestureUp]; 

    UISwipeGestureRecognizer *swipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; 
    swipeGestureDown.direction = UISwipeGestureRecognizerDirectionDown; 
    [imgViewBackGround addGestureRecognizer:swipeGestureDown]; 

    [self.viewScrollView addSubview:imgViewBackGround]; 
    [self.viewScrollView addSubview:subview]; 


} 

self.viewScrollView.contentSize = CGSizeMake(self.viewScrollView.frame.size.width * arrNews.count, self.viewScrollView.frame.size.height); 

pageControl.numberOfPages = arrNews.count; 
pageControl.currentPage = 0; 
+0

u需要对滚动型加手势 – iAnurag

回答

0

你需要实现这个委托方法

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 
0

试试这个:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeScreen:)]; 
swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown; 
[yourScrollView addGestureRecognizer:swipe]; 

//处理挥笔这里

- (void)didSwipeScreen:(UISwipeGestureRecognizer *)gesture 
{ 
    switch (gesture.direction) { 
    case UISwipeGestureRecognizerDirectionUp: 
     // you can include this case too 
     break; 
    case UISwipeGestureRecognizerDirectionDown: 
     // you can include this case too 
     break; 
    case UISwipeGestureRecognizerDirectionLeft: 
    case UISwipeGestureRecognizerDirectionRight: 
     // disable timer for both left and right swipes. 
     break; 
    default: 
     break; 
    } 
}