我有两个UIButton,第一个按钮将触发CustomeView的- beginAnimation,另一个将触发- endAnimation。当我依次快速按下这两个按钮时,比如begin -> end -> begin -> end -> begin -> end,我发现CADisplayLink无法停止。更重要的是,- rotate的火灾率超过60 fix,变成了60 -> 120 -> 180,就像我的主RunLoop中有多个CADisplaylink一样,那么还有什么可以修复的吗?而且我需要在视图的alpha值降到零之前保持CADisplaylink运行,所以我把[self.displayLink invalidate];放在完成块中,这可能会导致这个问题?
@interface CustomeView : UIView
@end
@implementation CustomeView
- (void)beginAnimation // triggered by a UIButton
{
[UIView animateWithDuration:0.5 animations:^{ self.alpha = 1.0; }];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)endAnimation // triggered by another UIButton
{
[UIView animateWithDuration:0.5 animations:^{ self.alpha = 0.0; } completion:^(BOOL finished) {
[self.displayLink invalidate];
}];
}
- (void)rotate
{
// ....
}发布于 2016-01-03 19:45:03
如果在-beginAnimation中的完成块运行之前调用-endAnimation --也就是说,在0.5秒动画完成之前--您将用新的self.displayLink覆盖旧的self.displayLink。之后,当完成块运行时,您将使新的显示链接失效,而不是旧的链接。
使用中间变量捕获self.displayLink的值,该值保存要使其无效的显示链接。此外,为了更好地度量,当您完成时,将self.displayLink设置为零。
- (void)beginAnimation // triggered by a UIButton
{
[UIView animateWithDuration:0.5 animations:^{ self.alpha = 1.0; }];
if (self.displayLink != nil) {
// You called -beginAnimation before you called -endAnimation.
// I'm not sure if your code is doing this or not, but if it does,
// you need to decide how to handle it.
} else {
// Make a new display link.
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotate)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
}
- (void)endAnimation // triggered by another UIButton
{
if (self.displayLink == nil) {
// You called -endAnimation before -beginAnimation.
// Again, you need to determine what, if anything,
// to do in this case.
} else {
CADisplayLink oldDisplayLink = self.displayLink;
self.displayLink = nil;
[UIView animateWithDuration:0.5 animations:^{ self.alpha = 0.0; } completion:^(BOOL finished) {
[oldDisplayLink invalidate];
}];
}
}https://stackoverflow.com/questions/34579665
复制相似问题