如何使UISwipeGestureRecognizer在UIScrollView中工作

时间:2016-01-09 12:01:44

标签: ios objective-c uiscrollview uikit

我所拥有的是viewDidLoad

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideHeaderBar:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
swipeUp.

[self.view addGestureRecognizer:swipeUp];

然后我在视图中有一个UIScrollView。出于某种原因,当我向上滚动滚动视图时,它不会调用select hideHeaderBar。对此有什么修正吗?

-(void)hideHeaderBar:(UISwipeGestureRecognizer*)swipeRecognizer{
    POPSpringAnimation *fadeButton = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerOpacity];
    fadeButton.toValue = @(0.0);
    fadeButton.springBounciness = 0.f;

    [settingsButton.layer pop_addAnimation:fadeButton forKey:@"fadeButton"];
    [heartButton.layer pop_addAnimation:fadeButton forKey:@"fadeButton"];
    [conversationsButton.layer pop_addAnimation:fadeButton forKey:@"fadeButton"];

    POPSpringAnimation *hideHeader = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    hideHeader.toValue = @(-10);
    hideHeader.springBounciness = 5.f;
    [headerBar.layer pop_addAnimation:hideHeader forKey:@"hideHeaderAnim"];
    POPSpringAnimation *hideBody = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    hideBody.toValue = [NSValue valueWithCGRect:CGRectMake(0, -10, screenSize.size.width, screenSize.size.height)];
    hideBody.springBounciness = 5.f;
    [homeScrollView.layer pop_addAnimation:hideBody forKey:@"hideBodyAnim"];


}

需要注意的一点是,UIScrollView位于子视图控制器中(位于UIScrollView内部)

[self addChildViewController:settingsViewController];
[homeScrollView addSubview:settingsViewController.view];
[settingsViewController.view setFrame:CGRectMake(0, 0, screenSize.size.width, screenSize.size.height)];
[settingsViewController didMoveToParentViewController:self];

2 个答案:

答案 0 :(得分:2)

当然,您可以使用滑动手势识别器,但滚动视图已经可以处理滑动。

更好 以及更合适在这里做的是将视图控制器设置为UIScrollViewDelegate并实现此方法:

scrollViewDidScroll:

要仅检测滚动上升的时间,可以向视图控制器添加一个如下所示的属性:

@property (nonatomic) CGFloat lastContentOffset;

并简单地执行以下操作:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.lastContentOffset > scrollView.contentOffset.y)
    {
        NSLog(@"Scrolling Up");
    }
    else if (self.lastContentOffset < scrollView.contentOffset.y)
    {
        NSLog(@"Scrolling Down");
    }

    self.lastContentOffset = scrollView.contentOffset.y;
}

(上面的代码我发现in this very related question

当出现“向上滚动”行时,就有时间隐藏标题栏了。

答案 1 :(得分:0)

因为我认为你设置的内容大小(高度)大于滚动视图高度所以当你向上滑动时,调用scrollview的默认滚动方法。并且你的hideHeaderBar没有被调用。所以要么你必须停止滚动或设置其内容高度少于滚动视图,但之后滚动视图意味着更少(它变得与视图相同)。所以我只是为你的测试说出来。