【问题标题】:UILongPressGestureRecognizer not working for all UIButtonsUILongPressGestureRecognizer 不适用于所有 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);
    }
}

【问题讨论】:

    标签: ios cocoa-touch uigesturerecognizer long-press


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

      【讨论】:

        猜你喜欢
        • 2012-08-06
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 2015-08-04
        • 1970-01-01
        相关资源
        最近更新 更多