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")
}
fonte
2013-10-07 13:03:04
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
@NSPratik ho modificato il codice, per il tuo caso controllalo –
Grazie Shan per il tuo aiuto. Venerato !!! – NSPratik