essayer cette
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");
}
pour le cas de NSPratik: u peut utiliser l'événement UIControlEventTouchUpOutside
Si le bouton de presse à long utilisateur et après un certain temps, au lieu de libérer le doigt, l'utilisateur se déplace son/son doigt hors des limites du bouton. ajoutez juste un autre événement.
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");
}
Version Swift
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")
}
Il travaille pour moi.Mais, il y a un scénario dans lequel cela ne fonctionnera pas. Si l'utilisateur appuie longuement sur le bouton et après un certain temps, au lieu de relâcher le doigt, l'utilisateur sortira son doigt hors des limites du bouton. Dans ce cas 'holdRelease' ne sera pas appelé ... – NSPratik
@NSPratik j'ai modifié le code, pour votre cas, vérifiez-le –
Merci Shan pour votre aide. Upvoted !!! – NSPratik