UIView动画仅在呈现静态图像时触发

时间:2014-02-14 17:50:14

标签: ios image animation core-animation avfoundation

我有一个我最初设置的照片应用程序,以便用户拍摄的照片在UIImageView上显示三秒钟,然后被隐藏并保存。它工作,但后来我决定更改它以使图像向右移动并离开屏幕,它可以工作但只有当我在方法中声明previewImage.image时动画被触发并且仅在我工作时插入静态图像(例如[UIImage imageNamed:@"image"];)而不是从相机拍摄的图像。我无法弄清楚为什么这会发生在我的生命中!无论如何,请帮助我。这是我正在使用的代码,请注意我将所有图像保存在另一种方法中,将它们保存在核心数据模型中:

这是我用来拍照的地方:

- (IBAction)takeSnapshot:(id)sender {

    // Void all previously declared instances
    AVCaptureConnection *videoConnection = nil;

    // Checks what video connection is currently being used (rear camera)
    // Sets videoConnection to that connection
    for (AVCaptureConnection *connection in self.captureManager.captureImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo])
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection)
            break;
    }

    // Captures the instance recorded from above
    [self.captureManager.captureImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                                    completionHandler: ^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
    {

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        [previewImage setImage:image];
        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            previewImage.image = image;
        } else {
            image = [UIImage imageNamed:@"Placeholder"];
        }
        previewImage.hidden = NO;

        if (error != nil) {
            [[[UIAlertView alloc] initWithTitle:error.localizedDescription
                                        message:error.localizedRecoverySuggestion
                                       delegate:nil
                              cancelButtonTitle:NSLocalizedString(@"OK", nil)
                              otherButtonTitles:nil]
            show];
        }

    }];

    if (event.title == nil) {
        [self noEventFound];
    } else {
        [self commitImageTakenAnimation];
        [self saveImageToFile];
    }
}

- (void)commitImageTakenAnimation {

    previewImage.image = [UIImage imageNamed:@"Placeholder"];
    previewImage.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    // Launch the chain of the bug animation at the bottom of viewDidAppear
    [self moveLeft:nil finished:nil context:nil];
}

下面是一个小小的凹凸,图像使它看起来更自然:

- (void)moveLeft:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    [UIView animateWithDuration:0.2
                          delay:0.5
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         CGRect previewImageStartMoveFrame = previewImage.frame;
                         previewImageStartMoveFrame.origin.x = -previewImageStartMoveFrame.size.width * .25;
                         [UIView setAnimationDelegate:self];
                         previewImage.frame = previewImageStartMoveFrame;
                         [UIView setAnimationDidStopSelector:@selector(moveRight:finished:context:)];
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"Move Left Done");
                     }];

}


- (void)moveRight:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    CGRect previewImageOffScreenFrame = previewImage.frame;
    previewImageOffScreenFrame.origin.x = previewImageOffScreenFrame.size.width * 2;

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [UIView setAnimationDelegate:self];
                         previewImage.frame = previewImageOffScreenFrame;
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Face right done");
                         [self saveImageToFile];

                         previewImage.hidden = YES;
                         previewImage.image = nil;
                 }];

}

另外,我注意到当播放动画时(再次,仅使用静态图像),似乎永远不会调用完成块,我从未在日志中收到消息,而previewImage永远不会被隐藏。

1 个答案:

答案 0 :(得分:1)

你为什么要设置animationDelegate?你不需要配备完成处理程序块的新动画。我删除那些和animationDidStop方法并直接从第一个完成句柄块中调用[self moveRight:etc:]