答案很可能是我不能按照我想要的方式混合东西,我会接受“正确的方法”来混合东西(可能是一个更复杂的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
等。 :)
答案 0 :(得分:5)
您只能与主线程上的视图层次结构进行交互。您不应该在全局调度队列上调用self.layer
。您只能在dispatch_get_main_queue()
上执行此操作。
您必须将日志记录重写为不循环,因为完成块也在主线程上运行,如果块正在运行,它将无法运行。
问题肯定是从非主线程访问图层引起的。我用视图子类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值永远不会改变。