spróbować
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");
}
dla przypadku NSPratik za: u można użyć zdarzenia UIControlEventTouchUpOutside
Jeśli przycisk użytkownik naciśnij długo i po pewnym czasie, a nie podnosząc palca, użytkownik przesunie jego/jej palec poza zasięgiem przycisku. po prostu dodaj jeszcze jedno wydarzenie.
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 Wersja
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")
}
To działa na mnie.Ale jest jeden scenariusz, w którym to nie zadziała. Jeśli użytkownik długo naciska przycisk i po pewnym czasie, zamiast zwalniać palec, użytkownik wyniesie palec z granic przycisku. W takim przypadku 'holdRelease' nie zostanie wywołany ... – NSPratik
@NSPratik zredagowałem kod, dla twojego przypadku sprawdź to –
Dzięki Shan za twoją pomoc. Wzniesione !!! – NSPratik