您只能与主线程上的视图层次结构进行交互。您不应该调用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 值永远不会改变。