答案可能是我无法按照自己的方式进行混合,而我会接受“正确的方式”进行混合(可能是更复杂的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
等:)
最佳答案
您只能与主线程上的视图层次结构进行交互。您不应在全局调度队列上调用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值永远不会改变。