首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >失效后CADisplayLink不能停止

失效后CADisplayLink不能停止
EN

Stack Overflow用户
提问于 2016-01-03 17:54:45
回答 1查看 749关注 0票数 0

我有两个UIButton,第一个按钮将触发CustomeView的- beginAnimation,另一个将触发- endAnimation。当我依次快速按下这两个按钮时,比如begin -> end -> begin -> end -> begin -> end,我发现CADisplayLink无法停止。更重要的是,- rotate的火灾率超过60 fix,变成了60 -> 120 -> 180,就像我的主RunLoop中有多个CADisplaylink一样,那么还有什么可以修复的吗?而且我需要在视图的alpha值降到零之前保持CADisplaylink运行,所以我把[self.displayLink invalidate];放在完成块中,这可能会导致这个问题?

代码语言:javascript
复制
@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
{
    // ....
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-03 19:45:03

如果在-beginAnimation中的完成块运行之前调用-endAnimation --也就是说,在0.5秒动画完成之前--您将用新的self.displayLink覆盖旧的self.displayLink。之后,当完成块运行时,您将使新的显示链接失效,而不是旧的链接。

使用中间变量捕获self.displayLink的值,该值保存要使其无效的显示链接。此外,为了更好地度量,当您完成时,将self.displayLink设置为零。

代码语言:javascript
复制
- (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];
        }];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34579665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档