如何绑定 CAConstraint 的比例?

发布于 2024-10-03 11:25:22 字数 1738 浏览 7 评论 0原文

我想绑定 CAConstraint 的比例,以便 CALayer 相对于其超级层的宽度可以指示(QTMovie 的)进度基于我在每秒触发的预定计时器方法中更新的变量。当进度为零时,CALayer 的宽度应为 0.0f;当进度完成时,CALayer 的宽度应为 100%(即 currentProgress == 1.0f)。我使用约束创建图层的方式如下:

self.progressLayer = [[[CALayer alloc] init] autorelease];

progressLayer.name = @"progressLayer";
progressLayer.backgroundColor = [Utilities blackColour];

[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinX relativeTo:@"superlayer" attribute:kCAConstraintMinX]];
[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintHeight relativeTo:@"superlayer" attribute:kCAConstraintHeight]];
[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY relativeTo:@"superlayer" attribute:kCAConstraintMidY]];
CAConstraint* constraint = [CAConstraint constraintWithAttribute:kCAConstraintWidth relativeTo:@"superlayer" attribute:kCAConstraintWidth scale:0.0f offset:0.0f];
[constraint bind:@"scale" toObject:[MediaPlayer sharedMediaPlayer] withKeyPath:@"currentProgress" options:nil];
[progressLayer addConstraint:constraint];

我遇到的问题是,当我更新每秒触发的方法中的 currentProgress 变量时, CALayer< /code> 根本不更新它的宽度。首先测试我的代码,我只是将进度值增加 0.01f 并且我正在记录数量并且它肯定正在更新。但是,当我根据按钮单击更新金额时,图层会正确更新。

为了更好地衡量,这就是我在 MediaPlayer 类中的 NSTimer 方法中更新变量的方式:

- (void) checkProgress:(NSTimer*)timer {
    self.currentProgress += 0.01f;
    NSLog(@"checkProgress: %f", currentProgress);
}

按下按钮后,我会像这样更新它:

self.currentProgress += 0.1f;

为什么不图层的宽度根据我的方法的变化进行更新,但在按下按钮时更新?有人有什么想法可以让我遵循来尝试解决这个问题吗?

谢谢!

I want to bind the scale of a CAConstraint so that a CALayer's width with respect to it's superlayer can indicate progress (of a QTMovie) based on a variable that I am updating in a scheduled timer method that fires every second. The width of the CALayer should be 0.0f when progress is zero and 100% the width of the superlayer when progress is complete (i.e. currentProgress == 1.0f). The way that I have created the layer with the constraint is as follows:

self.progressLayer = [[[CALayer alloc] init] autorelease];

progressLayer.name = @"progressLayer";
progressLayer.backgroundColor = [Utilities blackColour];

[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinX relativeTo:@"superlayer" attribute:kCAConstraintMinX]];
[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintHeight relativeTo:@"superlayer" attribute:kCAConstraintHeight]];
[progressLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY relativeTo:@"superlayer" attribute:kCAConstraintMidY]];
CAConstraint* constraint = [CAConstraint constraintWithAttribute:kCAConstraintWidth relativeTo:@"superlayer" attribute:kCAConstraintWidth scale:0.0f offset:0.0f];
[constraint bind:@"scale" toObject:[MediaPlayer sharedMediaPlayer] withKeyPath:@"currentProgress" options:nil];
[progressLayer addConstraint:constraint];

The problem that I have is that when I update the currentProgress variable in my method that is fired every second, the CALayer doesn't update it's width at all. To start with testing my code I am just incrementing the progress value by 0.01f and I am logging the amount and it is definitely updating. However, when I update the amount based on a button click, the layer updates correctly.

Just for good measure this is how I am updating the variable in my NSTimer method within my MediaPlayer class:

- (void) checkProgress:(NSTimer*)timer {
    self.currentProgress += 0.01f;
    NSLog(@"checkProgress: %f", currentProgress);
}

And on a button press I update it like so:

self.currentProgress += 0.1f;

Why isn't the width of the layer updating based on the changes in my method but updates on the button press? Does anyone have any ideas for me to follow to try and figure this out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2024-10-10 11:25:22

事实证明,通过单击按钮,我正在修改“兄弟/姐妹”层(添加到其超级层的另一个层),这似乎提示该层进行自身更新。通过不修改其他层,我所约束的层就没有更新。因此,约束 CAConstraint 的变量似乎不会使 CALayer 保持更新。也许这是因为 CAConstraint 没有比例的设置器。如果它有一个设置器,它将能够告诉图层它需要更新。所以我开始以不同的方式设置图层的宽度。

It turns out that with my button click I was modifying a "brother/sister" layer (another layer added to it's superlayer) which seems to have prompted the layer to do an update of itself. By not modifying the other layer, my layer that I was constraining just didn't update. So it seems that constraining a variable of a CAConstraint won't keep the CALayer updated. Maybe this is because CAConstraint doesn't have a setter for the scale. And if it had a setter it would be able to tell the layer that it needs to be updated. So I have gone about setting the width of my layer a different way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文