0

我有一个带有后置摄像头实时视图的子视图。将相机切换到前面时,我正在为其设置动画。因为我无法同步动画和切换,所以我用另一个纯黑色背景的视图覆盖了它。因为切换速度较慢,所以我想在执行切换后删除黑色子视图。但是当我这样做时,它不会消失。调试器显示我输入的所有 NSLog,当我在任何地方放置断点时,调试器甚至停止,因此代码是可以访问的。但是UIView根本没有修改,仍然保持黑色子视图。

有趣的事实:当我用顶部按钮关闭显示然后重新打开时,黑色视图消失了!

代码:

- (IBAction)cameraChanged:(id)sender {
NSLog(@"Camera changed");
UIView *animView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.videoPreviewView.frame.size.width, self.videoPreviewView.frame.size.height)];
animView.tag = 42;
[animView setBackgroundColor:[UIColor blackColor]];

[UIView transitionWithView:self.videoPreviewView duration:1.0
                    options:self.animate ? UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight
                animations:^{
                    [self.videoPreviewView addSubview:animView];
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
                        [self.captureManager toggleCamera];
                        NSLog(@"Done toggling");
                        [self done];
                    });
                }
                completion:^(BOOL finished){
                    if (finished) {
                        //
                    }
                }];
self.animate = !self.animate;
}

- (void)done {
for (UIView *subview in [self.videoPreviewView subviews]) {
    NSLog(@"In for");
    if (subview.tag == 42) {
        NSLog(@"removing");
        [subview removeFromSuperview];
    }
}
}

任何人都知道为什么会发生这种情况以及如何解决它?

4

1 回答 1

4

UIView 更新应该在主线程上运行。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
//BlahBlah
        dispatch_sync(dispatch_get_main_queue(), ^{
        //update your UIView
        });
    });
于 2013-04-17T09:25:36.163 回答