平移手势问题,UIButton 检测中的轻微移动 [英] pan gesture issue, slight movement within UIButton detection

查看:33
本文介绍了平移手势问题,UIButton 检测中的轻微移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了这个平移手势识别器来检测我对几个 UIBUtton 的触摸.这个想法是.我正在寻找能够将我的手指滑过所有按钮并在我触摸它们时触发每个按钮的能力.现在我可以滑过所有按钮并使用此代码触发声音.有一个问题正在发生,那就是当我用 NSLog 语句替换声音文件时,我在同一个按钮内用手指做的每一个微小的移动都会非常快速地一遍又一遍地重复声音.它会对最轻微的动作做出反应.

So I created this pan gesture recogniser to detect my touch on several UIBUttons. The idea is. I am looking for having the ability to slide my finger over all the buttons and trigger each and single one of them while I touch them. Right now I am able to slide over all the buttons and trigger the sound with this code. There is one problem that is occurring and that is when I replace the sound file with the NSLog statement, Every tiny little move I make with my finger within the same button keeps on repeating sound over and over again really fast. It reacts to the slightest movement.

如何在我的手指触摸按钮后只听到一次声音,并在我的手指再次触摸同一个按钮时能够再次播放相同的声音.几乎就像您在真正的钢琴上触摸或滑动手指时获得的效果一样.

How can I enable only hearing the sound one time after my finger touches the button and have the ability to play the same sound again when my finger touches the same button again. Pretty much the effect you get when you touch or slide your finger over a real piano.

谁能帮我解决这个问题?

Can anyone help me out with this?

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

推荐答案

保留一个 NSMutableArray 用于检测自上次触地事件以来播放了哪些声音(伪代码如下,请替换为正确的方法名称和签名):

Keep an NSMutableArray for detecting which sounds have been played since the last touch down event (pseudo code follows, please replace with proper method names and signatures):

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"];
}
}

这篇关于平移手势问题,UIButton 检测中的轻微移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆