2013-10-07 24 views
13

Voglio creare un UIButton che può essere tenuto premuto, quando tenuto premuto chiama l'azione "hold down" una volta. quando viene rilasciato, chiama l'azione "hold was release".UIButton con azione di mantenimento e azione di rilascio

Questo codice non funziona correttamente in quanto il tocco può muoversi all'interno del pulsante e gli eventi non vengono attivati ​​in modo corretto

[button handleControlEvent:UIControlEventTouchDown withBlock:^{ 
     [self performMomentaryAction:PXActionTypeTouchDown]; 
    }]; 
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^{ 
     [self performMomentaryAction:PXActionTypeTouchUp]; 
    }]; 

eventi di controllo maniglia si basa sull'attuazione blocco UIButton +

risposta

39

provare questo

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    aButton.frame = CGRectMake(xValue, yValue, 45, 45); 
    [aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown]; 
    [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside]; 


- (void)holdDown 
    { 
    NSLog(@"hold Down"); 
    } 

    - (void)holdRelease 
    { 
     NSLog(@"hold release"); 

    } 

per il caso di NSPratik: u possibile utilizzare l'evento UIControlEventTouchUpOutside Se premere il pulsante lunga utente e dopo qualche tempo, invece di rilasciare il dito, l'utente si muoverà il suo/il suo dito fuori dai limiti del pulsante. aggiungi solo un altro evento.

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    aButton.frame = CGRectMake(xValue, yValue, 45, 45); 
    [aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown]; 
    [aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside]; 
    [aButton addTarget:self action:@selector(holdReleaseOutSide) forControlEvents:UIControlEventTouchUpOutside]; //add this for your case releasing the finger out side of the button's frame 

//add this method along with other methods 
    - (void)holdReleaseOutSide 
    { 
    NSLog(@"hold release out side"); 
    } 

Swift versione

var aButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
    aButton.frame = CGRectMake(xValue,yValue, 45, 45) 
    aButton.setTitle("aButton", forState: UIControlState.Normal) 
    aButton.backgroundColor = UIColor.greenColor() 
    aButton.addTarget(self, action: Selector("holdRelease:"), forControlEvents: UIControlEvents.TouchUpInside); 
    aButton.addTarget(self, action: Selector("HoldDown:"), forControlEvents: UIControlEvents.TouchDown) 
    self.addSubview(testButton) 

    //target functions 
    func HoldDown(sender:UIButton) 
    { 
     println("hold down") 
    } 

    func holdRelease(sender:UIButton) 
    { 
     println("hold release") 
    } 
+0

E 'di lavoro per me.Ma, c'è uno scenario in cui questo non funzionerà. Se l'utente preme a lungo il pulsante e dopo un po 'di tempo, invece di rilasciare il dito, l'utente sposta il dito fuori dai limiti del pulsante. In tal caso, 'holdRelease' non verrà chiamato ... – NSPratik

+1

@NSPratik ho modificato il codice, per il tuo caso controllalo –

+0

Grazie Shan per il tuo aiuto. Venerato !!! – NSPratik

6

Prova questo

Add Touchdown, tocco Inside, ritoccare evento esterno al tasto ur

enter image description here

-(IBAction)theTouchDown:(id)sender 
{ 

    timer = [NSTimer scheduledTimerWithTimeInterval:0.2 
              target:self 
              selector:@selector(performFunctionality) 
              userInfo:nil 
    } 
-(IBAction)theTouchUpInside:(id)sender 
{ 
[timer invalidate]; 
timer = nil; 
[self performFunctionality]; 

} 


-(IBAction)theTouchUpOutside:(id)sender 
{ 
[timer invalidate]; 
timer = nil; 
} 

-(void)performFunctionality 
{ 
//write your logic 
} 
0

trascinare un pulsante da strumenti (riquadro) e fare clic destro sul it.A lista avverrà trova "ritoccare dentro" e fare clic e trascinare il punto di cerchio che si sulla parte anteriore del testo e spostarlo su viewcontroller.h e definire un nome e in viewcontroller.m farlo.

nslog("clicked in"); ripetere tutte le azioni per il tocco fuori e trovare un evento appropriata dalla lista

0

È necessario collegare a seguito di due eventi 1.Touch Giù, 2.Touch dentro ad esso è IBAction Metodi .E sarà da divertirsi in Interface Builder. Ma si può fare a livello di codice utilizzando anche addTarget: AZIONE: forControlEvents:

6

ho affrontato questo problema io stesso, e soprattutto abbiamo utilizzare questi eventi: -

// Questo evento funziona bene e gli incendi

[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown]; 

// Questo non scatta affatto

[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside]; 

Soluzione: -

uso a lungo Press Gesture Recognizer: -

UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)]; 
[aButton addGestureRecognizer:btn_LongPress_gesture]; 

Attuazione del gesto: -

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{ 


//as you hold the button this would fire 

if (recognizer.state == UIGestureRecognizerStateBegan) { 

    [self holdDown]; 
} 

//as you release the button this would fire 

if (recognizer.state == UIGestureRecognizerStateEnded) { 

    [self holdRelease]; 
} 
} 
+0

Questo approccio era l'unico che funziona con tutte le stesse risposte che ho trovato su StackOverflow (iOS 7.1., Xcode 5.1.). –

+0

Grazie .. e grande ha risolto il tuo problema – Ashish

1
**in Swift 3.0,** 

btnPasswordShow.addTarget(self, action:#selector(btnShowPasswordClickHoldDown), for: .touchDown) 

btnPasswordShow.addTarget(self, action:#selector(btnShowPasswordClickRelease), for: .touchUpInside) 

func btnShowPasswordClickHoldDown(){ 
    txtpassword.isSecureTextEntry = false 
} 

func btnShowPasswordClickRelease(){ 
    txtpassword.isSecureTextEntry = true 
}