2015-07-21 62 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; 
    } 
}