Question

I'm having a problem that I fear has a simple solution. Each time the 'createNewSetInDB:' method is called, a new UIButton* is created, assigned a target for when the user presses the button, and assigned a UILongPressGesture* for when a user long presses the button.

The 'openSet:' method is correctly being called when a user taps one of these buttons. The 'showHandles:' long press method is only being called for the LAST UIButton* that was created. So if the 'createNewSetInDB:' method gets called 4 times and therefore creates 4 UIButtons, the first three do not handle the UILongPressGesture. The 4th UIButton does.

Any ideas?

UILongPressGestureRecognizer *showHandlesLongPress;

- (void)viewDidLoad

{
    showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showHandles:)];
    showHandlesLongPress.minimumPressDuration = .5;
}

- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
    UIButton *newSet = [[UIButton alloc]initWithFrame:
                                   CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
                                                                 6,
                                                                 35,
                                                                 25)];

    [newSet addTarget:self action:@selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
    [newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
    [newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
    [newSet.layer setBorderWidth:1];
    [newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [newSet setTitle:[NSString stringWithFormat:@"%i",totalNumberOfSets] forState:UIControlStateNormal];
    [scrollviewMusic addSubview:newSet];
    [arrayofSetButtons addObject:newSet];
    [newSet addGestureRecognizer:showHandlesLongPress];

    if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];

}


- (void)showHandles:(UILongPressGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"long press");
        for (UIButton *but in arrayofSetButtons)
        {
            if (but.tag != gesture.view.tag)
            {
                but.alpha = .5;
            }
        }

        [scrollviewMusic bringSubviewToFront:lefthandle];
        [scrollviewMusic bringSubviewToFront:righthandle];
        lefthandle.hidden = NO;
        righthandle.hidden = NO;

        lefthandle.frame = CGRectMake(gesture.view.frame.origin.x - 1,
                                      gesture.view.frame.origin.y - gesture.view.frame.size.height,
                                      2, 50);
        righthandle.frame = CGRectMake((gesture.view.frame.origin.x - 1) + gesture.view.frame.size.width,
                                       gesture.view.frame.origin.y - gesture.view.frame.size.height,
                                       2, 50);
    }
}
Was it helpful?

Solution

You have to assign one UILongPressGestureRecognizer per UIButton. They can all point to the same method.

- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
    UIButton *newSet = [[UIButton alloc]initWithFrame:
                        CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
                                   6,
                                   35,
                                   25)];

    [newSet addTarget:self action:@selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
    [newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
    [newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
    [newSet.layer setBorderWidth:1];
    [newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [newSet setTitle:[NSString stringWithFormat:@"%i",totalNumberOfSets] forState:UIControlStateNormal];
    [scrollviewMusic addSubview:newSet];
    [arrayofSetButtons addObject:newSet];

    // Add gesture recognizer
    //
    UILongPressGestureRecognizer *showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showHandles:)];
    showHandlesLongPress.minimumPressDuration = .5;
    [newSet addGestureRecognizer:showHandlesLongPress];

    if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];

}

OTHER TIPS

A gesture recognizer can only be attached to one view at a time. You're only creating one gesture recognizer. Each time you create a button, you attach the existing gesture recognizer to the new button, which removes it from the prior button.

Create a new gesture recognizer for each new button.

- (void)createNewSetInDB:(BOOL)doWeAddItToTheDatabase
{
    UIButton *newSet = [[UIButton alloc]initWithFrame:
                                   CGRectMake((sliderMusic.frame.origin.x + imgWhiteLine.frame.size.width / 2) + (_musicPlayer.currentPlaybackTime * 10),
                                                                 6,
                                                                 35,
                                                                 25)];

    [newSet addTarget:self action:@selector(openSet:) forControlEvents:UIControlEventTouchUpInside];
    [newSet setTitleColor:[MobileMarcherVariables sharedVariableInstance].systemColor forState:UIControlStateNormal];
    [newSet.layer setBorderColor:[[MobileMarcherVariables sharedVariableInstance].systemColor CGColor]];
    [newSet.layer setBorderWidth:1];
    [newSet.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [newSet setTitle:[NSString stringWithFormat:@"%i",totalNumberOfSets] forState:UIControlStateNormal];
    [scrollviewMusic addSubview:newSet];
    [arrayofSetButtons addObject:newSet];

    UILongPressGestureRecognizer *showHandlesLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showHandles:)];
    showHandlesLongPress.minimumPressDuration = .5;
    [newSet addGestureRecognizer:showHandlesLongPress];

    if (doWeAddItToTheDatabase) [[NSNotificationCenter defaultCenter] postNotification:newSetNotification];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top