【问题标题】:pan gesture issue, slight movement within UIButton detection平移手势问题,UIButton 检测中的轻微移动
【发布时间】:2015-02-05 18:18:37
【问题描述】:

所以我创建了这个平移手势识别器来检测我对几个 UIBUtton 的触摸。这个想法是。我正在寻找能够在我触摸它们时将手指滑过所有按钮并触发每个按钮的能力。现在,我可以滑过所有按钮并使用此代码触发声音。有一个问题正在发生,那就是当我用 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");
}

【问题讨论】:

    标签: objective-c xcode


    【解决方案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"];
    }
    }
    

    【讨论】:

    • 感谢 ABHashmi!这开始有意义了!在这一点上,该数组仍然让我感到困惑,您是指实际的按钮 1、2、3,还是您在名为“按钮 1、2、3”的按钮下方调用“声音文件”,假设在该项目(我的声音被称为不同,只是想清楚我理解)等?因为按钮1、2、3此时只是IBOutlets,并且没有带有声音的IBAction,只是想确保我理解正确。
    • @MilesH。通过这个tutorial 可能会有所帮助
    • Miles,数组只是为了跟踪播放了哪些声音。它可以是您为每个按钮选择的任何字符串或 NSNumber 值。我的意思是,如果按下 button1,我可以添加字符串 @"yo",但这也意味着我将检查 @"yo" 是否存在于我的数组中。简单地说:a) 值存在:==> 声音已经播放。 b) 值不存在:==> 声音尚未播放。此外,每当我选择播放声音时,我都会将其对应的值添加到数组中,从而有效地将声音标记为已播放。
    • 嗯,好的。所以在这段代码之后 NSMutableArray *myPlayedSounds;我只是在方括号中添加我想要触发的任何声音,并用我的声音文件具有的任何名称替换您在代码中提到的按钮 1、2 等名称,对吗?感谢您提供 CaptJak 教程,通读它 atm。我也不擅长阅读 Objective-C,因为我在 Swift 中做我的项目,所以我试图同时翻译它,看看它在 swift 中是如何工作的。 :)
    • myPlayedSounds 始终保存名称/值/指示自用户最后一次将手指放在屏幕上以来已播放过哪些声音的任何内容。当您滑过并点击按钮 1 时,假设您播放了“sound1”并将字符串 @“sound1played”添加到数组中。然后你向前,向后滑动并再次点击 button1。但是这一次,当你检查 myPlayedSounds 中是否存在 @"sound1played" 时,它会告诉你字符串存在,这意味着 "sound1" 已经播放过了,所以不要再播放了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多