gpt4 book ai didi

ios - 让 UIPanGesture 和 UISwipeGesture 朝同一方向协同工作

转载 作者:行者123 更新时间:2023-11-29 02:56:17 26 4
gpt4 key购买 nike

我已将我的 UIView 设置为在平移 View 时调用一个方法,并在向上滑动 View 时调用其他方法。我的平移工作正常,如果我的 velocity.y 超过某个限制,我可以让我的平移保持禁用状态,但是当我的平移失败时,我永远无法让滑动 Action 起作用。我试过使用委托(delegate)方法,但运气不佳。查看了这个解决方案但没有运气:https://stackoverflow.com/a/21728621/1925859

- (void)viewDidLoad
{
[super viewDidLoad];

UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRec.delegate = self;
[panedView addGestureRecognizer:panRec];

UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeRec.delegate = self;
[panedView addGestureRecognizer:swipeRec];

[swipeRec requireGestureRecognizerToFail:panRec];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
if (ABS(velocity.y) > 200)
{

NSLog(@"Swipe actiavted");
return NO;
}
}
return YES;
}

- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}

最佳答案

如果您删除委托(delegate)方法,为滑动添加方向并更改 requireGestureRecognizerToFail 的接收器,那么它将起作用。请注意,平移必须比扫描慢得多才能被识别。

- (void)viewDidLoad
{
[super viewDidLoad];

UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panedView addGestureRecognizer:panRec];

UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[panedView addGestureRecognizer:swipeRec];

[panRec requireGestureRecognizerToFail:swipeRec];
}


- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}

- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"PAN");
}

关于ios - 让 UIPanGesture 和 UISwipeGesture 朝同一方向协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23876983/

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