[英]UILongPressGestureRecognizer not working for all UIButtons
我有一個問題,我擔心有一個簡單的解決方案。 每次調用'createNewSetInDB:'方法時,都會創建一個新的UIButton *,為用戶按下按鈕時分配一個目標,並為用戶長按該按鈕時分配一個UILongPressGesture *。
當用戶點擊這些按鈕之一時,將正確調用'openSet:'方法。 僅為創建的LAST UIButton *調用'showHandles:'長按方法。 因此,如果“ 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);
}
}
手勢識別器一次只能附加到一個視圖。 您僅創建一個手勢識別器。 每次創建按鈕時,都會將現有的手勢識別器附加到新按鈕上,從而將其從上一個按鈕中刪除。
為每個新按鈕創建一個新的手勢識別器。
- (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];
}
您必須為每個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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.