UILongPressGestureRecognizer 不适用于所有 UIButtons

Posted

技术标签:

【中文标题】UILongPressGestureRecognizer 不适用于所有 UIButtons【英文标题】:UILongPressGestureRecognizer not working for all UIButtons 【发布时间】:2014-02-06 21:05:44 【问题描述】:

我遇到了一个我担心有一个简单解决方案的问题。每次调用 'createNewSetInDB:' 方法时,都会创建一个新的 UIButton*,为用户按下按钮分配一个目标,并为用户长按按钮分配一个 UILongPressGesture*。

当用户点击这些按钮之一时,会正确调用“openSet:”方法。 'showHandles:' 长按方法只为创建的 LAST UIButton* 调用。因此,如果“createNewSetInDB:”方法被调用了 4 次并因此创建了 4 个 UIButton,则前三个不处理 UILongPressGesture。第 4 个 UIButton 可以。

有什么想法吗?

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

【问题讨论】:

【参考方案1】:

您必须为每个UIButton 分配一个UILongPressGestureRecognizer。它们都可以指向同一个方法。

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


【讨论】:

【参考方案2】:

手势识别器一次只能附加到一个视图。您只创建一个手势识别器。每次创建按钮时,都会将现有手势识别器附加到新按钮,这会将其从之前的按钮中移除。

为每个新按钮创建一个新的手势识别器。

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


【讨论】:

以上是关于UILongPressGestureRecognizer 不适用于所有 UIButtons的主要内容,如果未能解决你的问题,请参考以下文章