2016-04-13 8 views
-2

Ich habe nicht viel Erfahrung in Objective-C. Ich versuche, das Ende der Animation zu erhalten, nachdem der Benutzer eine Taste gedrückt hat. In viewDidLoad Das habe ich hinzugefügt:Get CompletionBlock für CABasicAnimation

UILongPressGestureRecognizer *recoginzer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)]; 
[_buttonStart addGestureRecognizer:recoginzer]; 

dann in dem Verfahren I setCompletionBlock verwendet, um zu bestimmen, wenn die Animation beendet ist, aber es funktioniert nicht.

-(void)onPress:(UILongPressGestureRecognizer*)longpress { 

if (longpress.state == UIGestureRecognizerStateBegan) { 

    circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(2, 2, _buttonStart.frame.size.width-4, _buttonStart.frame.size.height-4) cornerRadius:(_buttonStart.frame.size.width/2)-8].CGPath; 
    circle.fillColor = [UIColor clearColor].CGColor; 
    circle.strokeColor = [UIColor whiteColor].CGColor; 
    circle.lineWidth = 2.5; 
    [_buttonStart.layer addSublayer:circle]; 


    [CATransaction begin]; 
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    drawAnimation.duration   = 3.0; 
    drawAnimation.repeatCount   = 1.0; 
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
    drawAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 

    [CATransaction setCompletionBlock:^{ 
      NSLog(@"DONE"); 
     }]; 

    [CATransaction commit]; 


} else if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) { 

    [circle removeFromSuperlayer]; 
    NSLog(@"long press OUT"); 

} 
} 

Vielen Dank im Voraus für Ihre Unterstützung

Antwort

4

Sie haben setCompletionBlock zu verwenden, bevor die Animationen hinzufügen.

Vom docs (Hervorhebung von mir):

Das Objekt Abschluss-Block, der (auf dem Hauptthread) genannt werden garantiert ist, sobald alle Animationen anschließend durch diese Transaktion Gruppe hinzugefügt abgeschlossen haben (oder wurden entfernt.)

[CATransaction begin]; 

[CATransaction setCompletionBlock:^{ 
    NSLog(@"DONE"); 
}]; 

[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 

[CATransaction commit]; 
+0

Vielen Dank! Um den Block nicht in UIGestureRecognizerStateEnded aufzurufen, sind UIGestureRecognizerStateCancelled und UIGestureRecognizerState fehlgeschlagen Wenn die Animation nicht abgeschlossen ist [circle removeFromSuperlayer]; wie kann ich? – Joannes