gpt4 book ai didi

iphone - 停止 UIGestureRecognizer 调用选择器

转载 作者:行者123 更新时间:2023-11-28 22:43:50 26 4
gpt4 key购买 nike

我正在添加一些带有目标和选择器的 UIGestureRecognizers。我将只谈论一个,因为我敢肯定另一个将是相同的。

我添加了一个 UIPinchGestureRecognizer

UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer new];
[pinch addTarget:self action:@selector(pinchGestureDetected:)];
[self.view setMultipleTouchEnabled:YES];
[self.view addGestureRecognizer:pinch];

现在我的目标是在收到捏合手势时简单地调用此方法一次。但很明显,当人捏时,它会继续调用它。我将其用作页面导航的一部分,并将在检测到捏合时更新 View 。

所以在我的 -(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch 方法中,我将调用另一个方法。有点像......这是一个小 sudo

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1) layoutViewWithMoreDetail;
else layoutViewWithLessDetail;
}

所以我不希望它一直调用这个方法或者布局方法会一直调用。我想要一种布局/捏合手势。

有什么方法可以在确定比例后停止检测捏合?一路走来......

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1)
{
layoutViewWithMoreDetail;
stop receiving pinch gestures till this.gesture is finished;
}

我会使用 GestureDelegate 吗??

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1 && pinching == NO )
{
layoutViewWithMoreDetail;
pinching = YES;
}

然后在 Gesture 的委托(delegate)中结束 ... pinching = NO;

感谢您的帮助

最佳答案

UPinchGestureRecognizer 是一个连续手势 - 使用 if(pinch.state == UIGestureRecognizerStateBegan) 检测事件是否刚刚开始。

但是,这会导致触发事件的阈值较低。另一种方法是在您满意地触发手势后快速禁用和启用手势,如下所示:

-(void)pinchGestureDetected:(UIPinchGestureRecognizer)pinch
{
if (pinch.scale > 1)
{
//do your stuff here
pinch.enabled = NO;
pinch.enabled = YES;
}
}

这是因为如果您查看 documentation ,它指出:

enabled

A Boolean property that indicates whether the gesture recognizer is enabled. @property(nonatomic, getter=isEnabled) BOOL enabled

Discussion

Disables a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

关于iphone - 停止 UIGestureRecognizer 调用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772431/

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