0

I'm trying to create a view in an app with a button that if tapped 3 times something happens. I have looked at multiple questions and used multiple codes but I just cannot get it to work. This is my code:

#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");
}

Warning:

  1. Incompatible pointer types initializing 'UITouch *' with an expression of type 'NSArray *'

Errors:

  1. Use of undeclared identifier 'touch'
  2. Expected expression

How do I get rid of those errors and get this to function correctly?

Thanks!

Also if anyone wants to mark me down please will you comment to explain why so I can learn from it.

adolfosrs
  • 9,286
  • 5
  • 39
  • 67

1 Answers1

1

try to use these..

Write these in viewdidload..

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

here in above code addGestureRecognizer where you wants to perform that 3 taps..

Modify your handleTapGesture to these..

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

remove these..

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

OR

check these too..

write these in 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];

here when you tap 3 times on the button the below method will be called..

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

i hope it helps..

krushnsinh
  • 874
  • 1
  • 10
  • 11
  • thanks but I need it to be when the `iconsBtn` is tapped 3 times that that happens. How would I do that? –  Sep 30 '15 at 11:46
  • thanks I did but I got the error `use of undeclared identifier` –  Sep 30 '15 at 11:50