根据 UIGesture 识别器状态执行操作

发布于 2024-11-09 00:57:10 字数 924 浏览 0 评论 0原文

这个想法是让进程附加到 UIGestureRecognizer 状态,更具体地说,我想要的是将手势参数记录到可变数组中,当识别手势状态时以及当手势结束时输出可变数组。

最初我以为我可以使用两个 while 循环:

-(void)pincherAction:(UIPinchGestureRecognizer *)sender 
{  
   if ([sender state] == UIGestureRecognizerStateBegan) 
   {
      pinchGesture = YES;
      while (pinchGesture) 
      {
         if ([sender state] == UIGestureRecognizerStateEnded) 
         {
             pinchGesture = NO;
         } else {
            //do array stuff here
         }
      }
   }

   if ([sender state] == UIGestureRecognizerStateEnded) 
   {
        pinchGesture = NO;
        while (!pinchGesture) 
        {
            if ([sender state] == UIGestureRecognizerStateEnded) 
            {
                pinchGesture = YES;
            } else {
                 //output array here
            }
        }
    }
}

问题是一旦进入 while 循环,它就不会获取任何状态更改。我需要在单独的线程上运行这个循环吗?或者我做了一些根本错误的事情!?

The idea is to have processes attaches to UIGestureRecognizer states more specifically what I want is to record the gesture parameters into a mutable array, when the gesture state is recognized and when the gesture is ended output the mutable array.

Initially I thought I could use two while loops :

-(void)pincherAction:(UIPinchGestureRecognizer *)sender 
{  
   if ([sender state] == UIGestureRecognizerStateBegan) 
   {
      pinchGesture = YES;
      while (pinchGesture) 
      {
         if ([sender state] == UIGestureRecognizerStateEnded) 
         {
             pinchGesture = NO;
         } else {
            //do array stuff here
         }
      }
   }

   if ([sender state] == UIGestureRecognizerStateEnded) 
   {
        pinchGesture = NO;
        while (!pinchGesture) 
        {
            if ([sender state] == UIGestureRecognizerStateEnded) 
            {
                pinchGesture = YES;
            } else {
                 //output array here
            }
        }
    }
}

The problem is that once in the while loop it doesnt pick up any state changes. Would I need to run this loop on a separate thread? or am I doing something fundamentally wrong!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-11-16 00:57:10

我刚刚使用 UIPinchGestureRecognizer 创建了一个简单的项目。在 viewController 的 awakeFromNib 中设置它:

- (void)awakeFromNib
{
    UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                                              action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:rec];
    [rec release];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    NSLog(@"%f %f %d", sender.velocity, sender.scale, sender.state);
}

现在,当我只是进行一些捏合操作并将两个点移动一小段时,会经常调用捏操作方法:

2011-05-24 12:40:28.609 Pinch[1216:207] -3.506220 1.430036 2
2011-05-24 12:40:28.625 Pinch[1216:207] -2.277907 1.381398 2
2011-05-24 12:40:28.642 Pinch[1216:207] -2.738462 1.347324 2
2011-05-24 12:40:28.659 Pinch[1216:207] -2.095164 1.308440 2
2011-05-24 12:40:28.676 Pinch[1216:207] -2.383961 1.264628 2
2011-05-24 12:40:28.692 Pinch[1216:207] -2.357965 1.240309 2
2011-05-24 12:40:28.726 Pinch[1216:207] -1.192955 1.225726 2
2011-05-24 12:40:28.760 Pinch[1216:207] -0.397422 1.215989 2
2011-05-24 12:40:28.776 Pinch[1216:207] -0.291231 1.211164 2
2011-05-24 12:40:28.793 Pinch[1216:207] -0.366087 1.201407 2
2011-05-24 12:40:29.094 Pinch[1216:207] -0.366087 1.201407 3

并且会立即识别状态的更改。然而,它只记录您移动手指时的变化。

您不需要 while 循环。

I've just created a simple project with a UIPinchGestureRecognizer. Setting it up in the awakeFromNib for a viewController:

- (void)awakeFromNib
{
    UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                                              action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:rec];
    [rec release];
}

- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    NSLog(@"%f %f %d", sender.velocity, sender.scale, sender.state);
}

Now, when I just do some pinching and move the two points around for a small around the pinchAction method is called very often:

2011-05-24 12:40:28.609 Pinch[1216:207] -3.506220 1.430036 2
2011-05-24 12:40:28.625 Pinch[1216:207] -2.277907 1.381398 2
2011-05-24 12:40:28.642 Pinch[1216:207] -2.738462 1.347324 2
2011-05-24 12:40:28.659 Pinch[1216:207] -2.095164 1.308440 2
2011-05-24 12:40:28.676 Pinch[1216:207] -2.383961 1.264628 2
2011-05-24 12:40:28.692 Pinch[1216:207] -2.357965 1.240309 2
2011-05-24 12:40:28.726 Pinch[1216:207] -1.192955 1.225726 2
2011-05-24 12:40:28.760 Pinch[1216:207] -0.397422 1.215989 2
2011-05-24 12:40:28.776 Pinch[1216:207] -0.291231 1.211164 2
2011-05-24 12:40:28.793 Pinch[1216:207] -0.366087 1.201407 2
2011-05-24 12:40:29.094 Pinch[1216:207] -0.366087 1.201407 3

And a change in state is recognized immediately. However it only records changes when you move around the fingers.

You don't need the while loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文