简体   繁体   English

UILongPressGestureRecognizer不适用于所有UIButton

[英]UILongPressGestureRecognizer not working for all UIButtons

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. 每次调用'createNewSetInDB:'方法时,都会创建一个新的UIButton *,为用户按下按钮时分配一个目标,并为用户长按该按钮时分配一个UILongPressGesture *。

The 'openSet:' method is correctly being called when a user taps one of these buttons. 当用户点击这些按钮之一时,将正确调用'openSet:'方法。 The 'showHandles:' long press method is only being called for the LAST UIButton* that was created. 仅为创建的LAST UIButton *调用'showHandles:'长按方法。 So if the 'createNewSetInDB:' method gets called 4 times and therefore creates 4 UIButtons, the first three do not handle the UILongPressGesture. 因此,如果“ createNewSetInDB:”方法被调用了4次并因此创建了4个UIButton,则前三个不处理UILongPressGesture。 The 4th UIButton does. 第四个UIButton可以。

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

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

}

You have to assign one UILongPressGestureRecognizer per UIButton . 您必须为每个UIButton分配一个UILongPressGestureRecognizer 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];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM