pan gesture issue, slight movement within UIButton detection

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.

  • iphone CLLocationmanager Region monitoring callbacks not triggered
  • GoogleMapsUtils Framework wrapped in Cocoapods Build error on Xcode
  • IOS, How to add a custom SelectionIndicator to a UIPickerView?
  • Can multiple (two) persistent stores be used with one object model, while maintaining relations from one to the other?
  • Keeping dictionary keys of JSON Object in order in Objective C
  • Call a Swift Singleton from Objective-C
  • 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");
    }
    

    Solutions Collect From Internet About “pan gesture issue, slight movement within UIButton detection”

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