2014-02-06 61 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]; 

} 
相關問題