滑动手势不适用于UIScrollView和子视图

时间:2015-07-21 06:18:19

标签: ios objective-c iphone uiscrollview uigesturerecognizer

我试图在UIScrollView上识别向上/向下滑动手势。 ScrollView支持分页,每个页面都有一个UIImageView作为ScrollView的SubView。我尝试创建UISwipeGestureRecognizers并将其与ScrollView以及SubView UIImageView相关联。两者都不起作用。为什么呢?

如何可靠地上下滑动工作?我的要求是当用户向上滑动时在UIView上方显示横幅(ScrollView),并在用户向下滑动时将其关闭。

这是我目前的实施。

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;

2 个答案:

答案 0 :(得分:1)

您需要实现此委托方法

return x;

答案 1 :(得分: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;
   }
}