试图使用UIGesture目标c iOS的多个错误

时间:2015-09-30 11:31:40

标签: ios objective-c uigesturerecognizer semantics

我试图在应用中创建一个视图,其中有一个按钮,如果发生了3次点击。我已经查看了多个问题并使用了多个代码,但我无法让它工作。这是我的代码:

#import "ViewController3.h"
#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"

@interface ViewController3 ()
{
    AVAudioPlayer *_player;
}
@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *path = [NSString stringWithFormat:@"%@/sound.m4a", [[NSBundle mainBundle] resourcePath]];
    NSURL *soundUrl = [NSURL fileURLWithPath:path];
    _alarmPlayer.numberOfLoops = -1;
    _alarmPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    [ _player play];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    UIEvent *event;
    UITouch *touch = [[event allTouches] allObjects]; //warning
    tapGesture.numberOfTapsRequired = 3;
    [self.view addGestureRecognizer:tapGesture];

}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {
        // handling code
    }

}

- (IBAction)iconsBtn:(id)sender {
    if(touch.tapGesture) = 3) { //errors 1 and 2
        [ _alarmPlayer stop];
    }
    else
        NSLog(@"UGHHHHH");
}

警告:

  1. 不兼容的指针类型初始化&#39; UITouch *&#39;与 表达类型&#39; NSArray *&#39;
  2. 错误:

    1. 使用未声明的标识符&#39;触摸&#39;
    2. 预期表达
    3. 如何摆脱这些错误并使其正常运行?

      谢谢!

      如果有人想让我失望,请你发表评论解释为什么我可以从中学习。

1 个答案:

答案 0 :(得分:1)

尝试使用这些..

在viewdidload中写这些..

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 3;
[self.view addGestureRecognizer:tapGesture];

这里是上面的代码addGestureRecognizer,你想在那里执行3次点击..

将handleTapGesture修改为这些..

-(void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
    [ _alarmPlayer stop];
}

删除这些..

- (IBAction)iconsBtn:(id)sender 
{
    if(touch.tapGesture) = 3) 
    { //errors 1 and 2
    [ _alarmPlayer stop];
    }
else
    NSLog(@"UGHHHHH");
}

检查这些......

在viewdidload中写这些..

UIButton *btnsss=[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 60, 64)];
[btnsss setBackgroundColor:[UIColor redColor]];
[self.view addSubview:btnsss];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 3;
[btnsss addGestureRecognizer:tapGesture];

这里当您在按钮上点击3次时,将调用以下方法..

-(void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
   NSLog(@"hello");
   [ _alarmPlayer stop];
}

我希望它有所帮助......