gpt4 book ai didi

iphone - presentationLayer 值在动画期间不更新?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:51 25 4
gpt4 key购买 nike

答案很可能是我无法按照我尝试的方式混合事物,我会接受“正确的方式”来混合事物(可能是更复杂的 CAAnimation?我不知道)。我有什么:

__block BOOL animationComplete = FALSE;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveEaseInOut
animations:^{
[self setFrame:destRect];
}
completion:^(BOOL finished) {
animationComplete = YES;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];

然后轮询:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL), ^{
while (!animationComplete) {
CALayer *layer = self.layer.presentationLayer;
CGRect frame = [layer frame];
CGPoint point = [layer position];
float currentTranslation = [[layer valueForKeyPath:@"transform.translation.x"] floatValue];
NSLog(@"%.1f : %.1f : %.1f : %.1f : %.1f : %.1f",frame.origin.x, currentTranslation, frame.origin.x, layer.bounds.origin.x, layer.position.x, point.x);
}
});

我是不是抓错队列了?参数错误?我看错层了吗?上面是我尝试查看的值的散点图,在动画期间所有值都没有改变....

214.4 : 0.0 : 214.4 : 0.0 : 264.4 : 264.4
214.4 : 0.0 : 214.4 : 0.0 : 264.4 : 264.4
214.4 : 0.0 : 214.4 : 0.0 : 264.4 : 264.4
214.4 : 0.0 : 214.4 : 0.0 : 264.4 : 264.4
214.4 : 0.0 : 214.4 : 0.0 : 264.4 : 264.4

等:)

最佳答案

您只能在主线程上与 View 层次结构进行交互。您不应该在全局调度队列上调用 self.layer。您只能在 dispatch_get_main_queue() 上执行此操作。

您将不得不重写您的日志记录,使其不再像那样循环,因为完成 block 也在主线程上运行,如果您的 block 正在运行,它就无法运行。

更新

问题肯定是非主线程访问层导致的。我用 View 子类 MyView 创建了一个测试项目。我给了它这个方法:

- (IBAction)move {
CGRect destRect = self.frame;
destRect.origin.x = 300 - destRect.origin.x;

__block BOOL animationComplete = FALSE;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveEaseInOut
animations:^{
[self setFrame:destRect];
}
completion:^(BOOL finished) {
animationComplete = YES;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL), ^{
while (!animationComplete) {
dispatch_sync(dispatch_get_main_queue(), ^{
CALayer *layer = self.layer.presentationLayer;
CGRect frame = [layer frame];
CGPoint point = [layer position];
float currentTranslation = [[layer valueForKeyPath:@"transform.translation.x"] floatValue];
NSLog(@"%.1f : %.1f : %.1f : %.1f : %.1f : %.1f",frame.origin.x, currentTranslation, frame.origin.x, layer.bounds.origin.x, layer.position.x, point.x);
});
}
});
}

当这个方法运行时,我得到这样的输出:

2012-09-10 21:06:31.732 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.735 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.737 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.737 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.738 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.739 presentationLayerTest[1274:c07] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:06:31.739 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.740 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.741 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.741 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.742 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.743 presentationLayerTest[1274:c07] 20.1 : 0.0 : 20.1 : 0.0 : 100.1 : 100.1
2012-09-10 21:06:31.743 presentationLayerTest[1274:c07] 20.2 : 0.0 : 20.2 : 0.0 : 100.2 : 100.2
2012-09-10 21:06:31.744 presentationLayerTest[1274:c07] 20.2 : 0.0 : 20.2 : 0.0 : 100.2 : 100.2
2012-09-10 21:06:31.745 presentationLayerTest[1274:c07] 20.2 : 0.0 : 20.2 : 0.0 : 100.2 : 100.2
2012-09-10 21:06:31.745 presentationLayerTest[1274:c07] 20.2 : 0.0 : 20.2 : 0.0 : 100.2 : 100.2

...等等,X 值最终达到 280。

然后我注释掉了 dispatch_sync 行,所以层访问和日志记录发生在全局低优先级线程上,我得到如下输出:

2012-09-10 21:08:20.973 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.976 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.976 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.977 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.978 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.978 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.979 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.980 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.981 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.981 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.982 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.982 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.983 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0
2012-09-10 21:08:20.984 presentationLayerTest[1296:1b03] 20.0 : 0.0 : 20.0 : 0.0 : 100.0 : 100.0

...等等,X 值永远不会改变。

关于iphone - presentationLayer 值在动画期间不更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12361552/

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