[英]How to implement method: - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
[英]How to implement animationdidstop
晚上好!
我有一段和平的代碼,但是沒有調用animationdidstop方法。 我無法確定為什么它不起作用。 我嘗試了許多解決方案。
-(IBAction)MakeCircle:(id)sender{
// Add to parent layer
[self.view.layer addSublayer:circle];
// Configure animation
drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 5.0;
drawAnimation.repeatCount = 1.0;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// Add the animation
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if(anim == [self.view.layer animationForKey:@"drawCircleAnimation"]){
Label.text = [NSString stringWithFormat:@"Loser"];
}
}
謝謝!!
您沒有將自己設置為動畫的委托人。 在添加動畫之前添加以下行:
drawAnimation.delegate = self;
哦。 我確切地知道問題是什么。 提交動畫時,系統會復制動畫對象,因此在完成例程中它不會是同一對象。 嘗試向動畫添加唯一鍵,然后檢查該鍵,而不是檢查它是否與您提交的動畫對象相同。
例如:
drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
[drawAnimation setValue: @"mydrawCircleAnimation" forKey: @"animationKey"];
//The rest of your animation code...
然后在您的animationDidStop中:
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if([anim valueForKey: @"animationKey"
isEqualToString: @"mydrawCircleAnimation"])
{
Label.text = [NSString stringWithFormat:@"Loser"];
}
}
還有一種更清晰的方法來處理動畫完成代碼。 您可以將一個塊附加到動畫上,然后編寫一個常規的animationDidStop:completion:方法來查找該塊並在找到該塊時調用它。 在此線程中查看我的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.