State aggiungendo lungo regolatore gesto di rubinetto al UIView che ha le UIButtons come subviews? Se è così, qualcosa sulla falsariga dell'approccio di @Magic Bullet Dave è probabilmente la strada da percorrere.
Un'alternativa è quella di sottoclasse UIButton e aggiungere a ogni UIButton un longTapGestureRecogniser. È quindi possibile ottenere il pulsante per fare ciò che ti piace. Ad esempio, potrebbe inviare un messaggio che si identifica con un controller di visualizzazione. Il seguente frammento illustra i metodi per la sottoclasse.
- (void) setupLongPressForTarget: (id) target;
{
[self setTarget: target]; // property used to hold target (add @property and @synthesise as appropriate)
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:button action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void) longPress: (UIGestureRecognizer*) recogniser;
{
if (![recogniser isEnabled]) return; // code to prevent multiple long press messages
[recogniser setEnabled:NO];
[recogniser performSelector:@selector(setEnabled:) withObject: [NSNumber numberWithBool:YES] afterDelay:0.2];
NSLog(@"long press detected on button");
if ([[self target] respondsToSelector:@selector(longPressOnButton:)])
{
[[self target] longPressOnButton: self];
}
}
Nel vostro controller della vista si potrebbe avere il codice qualcosa di simile:
- (void) viewDidLoad;
{
// set up buttons (if not already done in Interface Builder)
[buttonA setupLongPressForTarget: self];
[buttonB setupLongPressForTarget: self];
// finish any other set up
}
- (void) longPressOnButton: (id) sender;
{
if (sender = [self buttonA])
{
// handle button A long press
}
if (sender = [self buttonB])
{
// handle button B long press
}
// etc.
}
if ([[target auto] respondsToSelector: @selector (longPressOnButton :)]) { [[target auto] longPressOnButton: self]; } questo if-block non si compila ... ha dato un errore come: nessun metodo di istanza noto come ... – trillions
L'ho capito ... ha aggiunto un protocollo ed è stato corretto. Grazie.. – trillions