所以我创建了这个平移手势识别器来检测我对几个UIBUttons的触摸。这个想法是。我正在寻找能够在我触摸它们的同时将手指滑过所有按钮并触发每个按钮并单击其中一个按钮的能力。现在我可以滑过所有按钮并使用此代码触发声音。有一个问题正在发生,那就是当我用NSLog语句替换声音文件时,我用手指在同一个按钮内做的每一个小小动作都会一次又一次地重复声音。它会对最轻微的动作作出反应。
如何在手指触摸按钮后仅启用一次听到声音,并且当我的手指再次触摸同一按钮时,能够再次播放相同的声音。当你用手指轻触真钢琴时,你会得到很多效果。
任何人都可以帮我解决这个问题吗?
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self.view addGestureRecognizer:pan];
}
//Method to handle the pan:
-(void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
//create a CGpoint so you know where you are touching
CGPoint touchPoint = [gesture locationInView:self.view];
//just to show you where you are touching...
NSLog(@"%@", NSStringFromCGPoint(touchPoint));
//check your button frame's individually to see if you are touching inside it
if (CGRectContainsPoint(self.button1.frame, touchPoint))
{
NSLog(@"you're panning button1");
}
else if(CGRectContainsPoint(self.button2.frame, touchPoint))
{
NSLog(@"you're panning button2");
}
else if (CGRectContainsPoint(self.button3.frame, touchPoint))
{
NSLog(@"you're panning button3");
}
答案 0 :(得分:1)
保留一个NSMutableArray,用于检测自上次触碰事件以来播放的声音(伪代码如下,请替换为正确的方法名称和签名):
NSMutableArray *myPlayedSounds;
void touchDown:(UITouch *) touch
{
//Empty played sounds list as soon as a touch event is sensed
[myPlayedSounds removeAllObjects];
}
-(void)handlePanGesture:(UIPanGestureRecognizer *)gesture
{
//create a CGpoint so you know where you are touching
CGPoint touchPoint = [gesture locationInView:self.view];
//just to show you where you are touching...
NSLog(@"%@", NSStringFromCGPoint(touchPoint));
//check your button frame's individually to see if you are touching inside it
if (CGRectContainsPoint(self.button1.frame, touchPoint) && [myPlayedSounds containsObject:@"button1"] == NO)
{
NSLog(@"you're panning button1");
[myPlayedSounds addObject:@"button1"];
}
else if(CGRectContainsPoint(self.button2.frame, touchPoint) && [myPlayedSounds containsObject:@"button2"] == NO)
{
NSLog(@"you're panning button2");
[myPlayedSounds addObject:@"button2"];
}
else if (CGRectContainsPoint(self.button3.frame, touchPoint) && [myPlayedSounds containsObject:@"button3"] == NO)
{
NSLog(@"you're panning button3");
[myPlayedSounds addObject:@"button3"];
}
}