【问题标题】:How to make UISwipeGestureRecognizer work in a UIScrollView如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作
【发布时间】:2016-01-09 12:01:44
【问题描述】:

我在viewDidLoad中拥有的是这个:

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

[self.view addGestureRecognizer:swipeUp];

然后我在视图中有一个UIScrollView。出于某种原因,当我在滚动视图上向上滚动时,它不会调用选择 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];

【问题讨论】:

标签: ios objective-c uiscrollview uikit


【解决方案1】:

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

这里可能更好更合适的做法是将您的视图控制器设置为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

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

【讨论】:

  • 太棒了!它比我想要的更多地调用我的 hideHeaderBar,但它有效。此外,当它反弹到顶部时,它会显示HeaderBar。
  • 如果您添加了“lastTimeCalledHideHeaderBar”属性,您可能会想出一种方法来限制调用 hideHeaderBar 的频率(例如,仅在一段时间后调用 hideHeaderBar 的功能)。
  • 完美!我已经将它设为“lastTimeCalledHideHeaderBar”一个布尔值,并只做一个 if 语句以避免多次调用它。
  • 还使用 - scrollViewWillEndDragging:withVelocity:targetContentOffset: 和速度属性来检测向上或向下滚动以修复反弹!
【解决方案2】:

因为我认为您设置的 content-size(height) 大于滚动视图的高度,所以当您向上滑动时,会调用滚动视图的默认滚动方法。并且你的 hideHeaderBar 没有被调用。因此,要么您必须停止滚动,要么将其内容高度设置为小于滚动视图,但此后滚动视图的含义将减少(它与视图相同)。所以我只是为了你的测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    相关资源
    最近更新 更多