gpt4 book ai didi

iphone - CAKeyframeAnimation完成后如何指定选择器?

转载 作者:行者123 更新时间:2023-12-03 18:19:30 25 4
gpt4 key购买 nike

我正在使用 CAKeyframeAnimation 沿着 CGPath 为 View 设置动画。动画完成后,我希望能够调用其他方法来执行另一个操作。有什么好的办法吗?

我已经研究过使用 UIView 的 setAnimationDidStopSelector:,但是从文档来看,这似乎仅适用于在 UIView 动画 block (beginAnimations 和 commitAnimations)中使用时。为了以防万一,我也尝试了一下,但似乎不起作用。

这里有一些示例代码(这是在自定义 UIView 子类方法中):

// These have no effect since they're not in a UIView Animation Block
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 1.0f;

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.center.x, self.center.y);

// add all points to the path
for (NSValue* value in myPoints) {
CGPoint nextPoint = [value CGPointValue];
CGPathAddLineToPoint(path, NULL, nextPoint.x, nextPoint.y);
}

pathAnimation.path = path;
CGPathRelease(path);

[self.layer addAnimation:pathAnimation forKey:@"pathAnimation"];

我认为应该可行的解决方法,但似乎不是最好的方法,是使用 NSObject 的 PerformSelector:withObject:afterDelay:。只要我将延迟设置为等于动画的持续时间,那就应该没问题。

还有更好的办法吗?谢谢!

最佳答案

或者您可以将动画括起来:

[CATransaction begin];
[CATransaction setCompletionBlock:^{
/* what to do next */
}];
/* your animation code */
[CATransaction commit];

并设置完成 block 来处理您需要做的事情。

关于iphone - CAKeyframeAnimation完成后如何指定选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2468166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com