UIPanGestureRecognizer在触摸时立即执行某些操作

时间:2013-07-12 18:12:57

标签: ios uigesturerecognizer uipangesturerecognizer

我是ios的新手,所以如果我遗漏了一些明显的东西,我会提前道歉。

我正在制作一个拼图,我希望单个拼图在触摸时增加尺寸并减少放手。

目前我有:

-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{
  if(recognizer.state == UIGestureRecognizerStateBegan)
  else if(recognizer.state == UIGestureRecognizerStateEnded)
}

当平底锅开始时(也是状态开始时)拼图块增加尺寸,当平底锅结束时(如预期的那样)尺寸减小。一旦用户触摸到这件作品并且在拼图块移动之前,我希望尺寸增大。选择图块时,可以在Words With Friends中看到这一点。

我试过了

-(IBAction)handleTap:(UITapGestureRecognizer *)recognizer{
  if(recognizer.state == UIGestureRecognizerStateBegan)
  else if(recognizer.state == UIGestureRecognizerStateEnded)
}

只有在手指抬起后才会增加拼图。

我的问题:

一旦手指触摸拼图,然后继续平移手势,是否有办法增加拼图的大小。

提前谢谢。

7 个答案:

答案 0 :(得分:28)

我也需要这样做,杰克的建议对我来说非常合适。如果它有助于将来遇到这个问题的任何人,这里是UIPanGestureRecognizer的子类实现(标题保持不变):

#import "ImmediatePanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation ImmediatePanGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    self.state = UIGestureRecognizerStateBegan;
}

@end

这就是您所需要的一切 - 只要您将手指放在视图上,只要您将手指向任意方向移动一个点就会立即更新,并提供常规{{1}的功能} {(如UIPanGestureRecognizertranslationInView)在普通人被解雇之前,所有这些都不会破坏任何现有功能。

答案 1 :(得分:7)

我已经实施了George WS的答案。通过一些测试,我意识到在初始触摸之后但在初始触摸结束之前发生的其他触摸事件未得到正确处理。这是我更新的实现。它有点幼稚,但可以防止UIGestureRecognizerStateBegan多次发生的奇怪行为。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.state >= UIGestureRecognizerStateBegan) {
        return;
    }

    [super touchesBegan:touches withEvent:event];
    self.state = UIGestureRecognizerStateBegan;
}

答案 2 :(得分:7)

Swift 3/4解决方案

import UIKit.UIGestureRecognizerSubclass

class InstantPanGestureRecognizer: UIPanGestureRecognizer {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        if (self.state == UIGestureRecognizerState.began) { return }
        super.touchesBegan(touches, with: event)
        self.state = UIGestureRecognizerState.began
    }

}

答案 3 :(得分:2)

根据the documentation UIPanGestureRecognizer,只有当“允许的最小手指数(minimumNumberOfTouches)移动到足以被视为平底锅”时,才输入UIGestureRecognizerStateBegan。如果您想在触摸后立即发生某些事情,您可以尝试继承UIPanGestureRecognizer并过度使用touchesBegan:withEvent:

答案 4 :(得分:1)

感谢George WS&amp;德里克·海瑟薇

Swift 2.2

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
  if (self.state == UIGestureRecognizerState.Began) {
    return
  }
  super.touchesBegan(touches, withEvent: event)
  self.state = UIGestureRecognizerState.Began;
}

你必须添加到你的Bridging-Header:

#import <UIKit/UIGestureRecognizerSubclass.h>

答案 5 :(得分:1)

我绘制了一个UIScrollView的视图。我使用UIPanGestureRecognizer在视图中绘制。此UIPanGestureRecognizer的minimumNumberOfTouches和maximumNumberOfTouches设置为1。

我使用scrollView中的UIPinchGestureRecognizer和UIPanGestureRecognizer进行平移和缩放。 scrollView的UIPanGestureRecognizer的minimumNumberOfTouches和maximumNumberOfTouches设置为2。

我使用了George WS的解决方案,并且注意到绘图开始很快,但是很难识别缩放时的捏合手势和平移时的两指平移手势。我略微更改了解决方案,并使用touchesMoved识别绘图的开始。缩放和平移效果更好。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
    super.touchesMoved(touches, with: event)
    state = UIGestureRecognizer.State.began
}

答案 6 :(得分:0)

我正在尝试做类似的事情,我一直在努力的解决方案是使用视图的touchesBegan:withEvent:来执行我想要在用户触摸屏幕的瞬间发生的动作。然后,一旦触摸成为手势,手势处理程序就会接管。

相关问题