1

我在用三个手势滑动时遇到问题

在我的 .m 中:

- (IBAction)click:(id)sender {

    [_text setText:@"Hello World"];
}

- (IBAction)resetText:(id)sender {

    [_text setText:@"Reset"];
}

当我点击屏幕时,输出消息“Hello World”将显示在标签上,当我从上到下滑动三个手指时它应该显示“重置”,但它崩溃了

奇怪的是,当我将 IBAction 的名称从“resetText”更改为例如“reset”或任何不带大写字母的名称时。任何大写字母都会崩溃

这是 Xcode 文件

4

2 回答 2

0

我更新了您的视图控制器,现在它适用于您想要的所有手势。请检查并告诉我。

- (void)viewDidLoad{
[super viewDidLoad];
//gesture for tap
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
[self.view addGestureRecognizer:tap];

//gesture for right swipe
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release]; 

//gesture for right swipe
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
[leftRecognizer release];}


- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer{ [_text setText:@" World"]; }

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer{ [_text setText:@"World"]; }

- (void)click{ [_text setText:@"Hello World"]; }
于 2013-07-09T08:43:09.307 回答
0

我查看了您的示例项目,似乎:

  • 崩溃仅发生在实际设备上
  • 仅当滑动手势需要 3 次或更多次触摸时才会发生崩溃

在我看来,UIGestureRecognizer当使用 Interface Builder 添加时,这就像类中的错误,因此您现在无能为力。我向 Apple 提交了一份雷达 (#14399827) 来描述这个问题。您可能也应该这样做


但是,您可以通过在代码中而不是像现在这样在情节提要中创建手势识别器来解决此错误。

从情节提要中删除手势识别器(完全删除),然后将其添加到viewDidLoad视图控制器中的方法中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UISwipeGestureRecognizer *recogniser = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(resetText:)];
    [recogniser setDirection:UISwipeGestureRecognizerDirectionDown];
    [recogniser setNumberOfTouchesRequired:3];
    [self.view addGestureRecognizer:recogniser];
}

我知道这并不理想,因为在某些情况下将视图控制器直接添加到情节提要可能更方便,但不幸的是,由于 Apple 的实现中存在错误,您目前似乎无法这样做。

于 2013-07-10T08:39:54.563 回答