三个手势让我的iPhone崩溃了

时间:2013-07-09 06:53:07

标签: iphone objective-c swipe uiswipegesturerecognizer

我有三个手势滑动的问题

在我的.m:

- (IBAction)click:(id)sender {

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

- (IBAction)resetText:(id)sender {

    [_text setText:@"Reset"];
}

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

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

this is the Xcode file

2 个答案:

答案 0 :(得分: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"]; }

答案 1 :(得分:0)

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

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

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


但是,您可以通过在代码中而不是在故事板中创建手势识别器来解决此问题。

从故事板中删除手势识别器(完全删除),然后将其添加到视图控制器中的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的实现中存在错误,您现在似乎无法做到这一点。