动画和删除NSImageView对象失败了吗?

时间:2013-08-13 09:49:03

标签: macos cocoa animation core-animation catransaction

我的应用情况:
我现在有一个显示动态图片的应用程序。我想捕获当前图像并将其保存到光盘。

我的目标:
我正在制作动画视觉效果:在顶部添加图像视图,然后将其从原始帧动画到CGRecrZero。最后,在动画完成后从vie层次结构中删除此图像视图。


现在问题:
第一次触发动作,似乎有效。但是当第二次触发它时,图像视图不会动画并保持所有子视图的顶部。


视图控制器的代码(使用ARC):

首先,这是触发快照的IBAction:

- (IBAction)actionSaveSnapshot:(id)sender
{
    ...    // Save the image file. This works everytime.

    NSThread *tempThread = [[NSThread alloc] initWithTarget:self
                                                   selector:@selector(threadSimulateScreenCapture:) 
                                                     object:nil];
    if (tempThread)
    {
        [tempThread start];
        tempThread = nil;     // Simply release it 
    }
}

以下是主题:

- (void)threadSimulateScreenCapture:(id)arg
{@autoreleasepool {
    NSImageView __strong *subImageView;
    subImageView = [[NSImageView alloc] init];
    NSRect subRect = [_imageView frame];    // the dynamic image view which I want to snapshot with
    const CGFloat animationTime = 0.4;

    [subImageView setWantsLayer:YES];
    [subImageView setImageScaling:NSImageScaleAxesIndependently];
    [subImageView setImage:[_imageView image]];

    dispatch_async(dispatch_get_main_queue(), ^{
        [CATransaction begin];
        [self.view addSubview:subImageView];
        [subImageView setFrame:subRect];
        [subImageView setNeedsDisplay:YES];
        [CATransaction commit];
    });

    [NSThread sleepForTimeInterval:0.01];
    dispatch_async(dispatch_get_main_queue(), ^{
        [CATransaction begin];
        [CATransaction setAnimationDuration:animationTime];
        [subImageView.layer setFrame:CGRectZero];
        [CATransaction commit];
    });

    [NSThread sleepForTimeInterval:(NSTimeInterval)animationTime];    // wait for end of animation

    dispatch_async(dispatch_get_main_queue(), ^{
        [subImageView removeFromSuperview];
    });

    NSLog(@"SubView Count: %@\n%@", self.view.subviews, subImageView);  // MARK. described below
    subImageView = nil;    // release the image
}} // ENDS: thread and aotureleasepool

每次程序运行到“MARK”行并打印子视图数组时,很明显subImageView未从superview中删除。每次线程结束时,都会出现“已删除的线程与未提交的CATransaction ....”。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我可以解决自己的问题:在[subImageView setFrame:subRect];的第一行,插入另一行:[subImageView.layer setFrame:subRect];

同时,在“addSubview:”方法之前添加另一行:[self.view setWantsLayer:YES];

看起来很有效。但我不知道为什么。为什么我应该设置视图和图层的框架?