2014-02-06 35 views
0

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

当用户点击其中一个按钮时,正确调用'openSet:'方法。 'showHandles:'长按方法只是为创建的最后一个UIButton *调用。因此,如果'createNewSetInDB:'方法被调用4次并因此创建了4个UIButton,则前三个不会处理UILongPressGesture。第四个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); 
    } 
} 

回答

2

您必须分配每一个UIButtonUILongPressGestureRecognizer。他们都可以指向相同的方法。

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

} 
+0

嗯,我感到很蠢 - 非常感谢。 –

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

} 
相关问题