gpt4 book ai didi

ios - 如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作

转载 作者:行者123 更新时间:2023-11-28 21:33:51 24 4
gpt4 key购买 nike

我在 viewDidLoad 中有这个:

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

[self.view addGestureRecognizer:swipeUp];

然后我在 View 中有一个 UIScrollView。出于某种原因,当我在 ScrollView 上向上滚动时,它不会调用选择 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 位于 subview Controller 中(位于 UIScrollView 本身内)

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

最佳答案

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

这里可能更好更合适做的是将您的 View Controller 设置为 UIScrollViewDelegate 并实现此方法:

scrollViewDidScroll:

要仅检测滚动向上的时间,您可以向 View Controller 添加一个如下所示的属性:

@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 )

当“向上滚动”行出现时,是时候隐藏您的标题栏了。

关于ios - 如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34693210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com