我试图在应用中创建一个视图,其中有一个按钮,如果发生了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");
}
警告:
错误:
如何摆脱这些错误并使其正常运行?
谢谢!
如果有人想让我失望,请你发表评论解释为什么我可以从中学习。
答案 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];
}
我希望它有所帮助......