2011-02-09 65 views
0

我有一个网格的各种UIButtons(5 x 5)...现在我有一个UIControlEventTouchUpInside ..这意味着当用户想要选择各种按钮需要按每一个,一个一个...UIButton网格激活同时拖动

当用户在各种按钮上拖动手指时,如何激活按钮。

这里是我使用的代码:

for (i = 0; i < num_caselles; i++) 
{ 
    lletra = [[UIButton alloc] initWithFrame:CGRectMake(POS_H, POS_V, mida_boto, mida_boto)]; 
    [botones addObject: lletra]; 
    [lletra setTitle: [caselles objectAtIndex: i] forState: UIControlStateNormal]; 
    lletra.tag = i; 
    [lletra addTarget:self action:@selector(lletraPitjada:) forControlEvents: UIControlEventTouchUpInside]; 
} 

回答

0

好吧我终于解决了这个办法:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event touchesForView:self] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 


    for(UIButton*boton in botones) 
    { 
    if(CGRectContainsPoint([boton frame], location) && boton.tag != boton_anterior) 
    { 
     boton_anterior = boton.tag; 
     [self lletraPitjada:boton]; 
    } 
    } 

} 

我重写/评论按钮组的行动,因为没有工作对我来说:

//[lletra addTarget:self action:@selector(lletraPitjada:) forControlEvents: UIControlEventTouchDragEnter]; 

和去活的用户交互,因为UITouch不喜欢按钮:

lletra.userInteractionEnabled = NO; 

而且voilà...所有工作都很完美...

0

您也可以反应:

UIControlEventTouchDragEnter或 UIControlEventTouchDragExit

处理这些情况。

+0

是的我也试过这个事件,但没有工作,只有第一个按钮响应拖动,其他我不受影响,而我拖过去... – david 2011-02-11 08:31:06