我遇到了一个我担心有一个简单解决方案的问题。每次调用'createNewSetInDB:'方法时,都会创建一个新的UIButton *,为用户按下按钮时分配一个目标,并在用户长按按钮时为其分配UILongPressGesture *。
当用户点击其中一个按钮时,正确调用'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);
}
}
答案 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];
}
答案 1 :(得分:2)
您必须为UILongPressGestureRecognizer
分配一个UIButton
。他们都可以指出相同的方法。
- (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];
}