Ios UIView动画仅在显示静态图像时激发

Ios UIView动画仅在显示静态图像时激发,ios,image,animation,core-animation,avfoundation,Ios,Image,Animation,Core Animation,Avfoundation,我有一个我最初设置的拍照应用程序,用户拍摄的照片在UIImageView上显示了三秒,然后被隐藏和保存。它起作用了,但后来我决定将其更改为使图像向右移动并离开屏幕。它只在我以动画触发的方法声明previewImage.image时起作用,并且只在我插入静态图像时起作用(例如[UIImage imagename:@“image”];)而不是从相机拍摄的图像。我不明白为什么这会发生在我的生命中!请尽你所能帮助我。以下是我正在使用的代码,请注意,我使用另一种方法保存所有图像,该方法将图像保存在核心数据

我有一个我最初设置的拍照应用程序,用户拍摄的照片在UIImageView上显示了三秒,然后被隐藏和保存。它起作用了,但后来我决定将其更改为使图像向右移动并离开屏幕。它只在我以动画触发的方法声明
previewImage.image
时起作用,并且只在我插入静态图像时起作用(例如
[UIImage imagename:@“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
也从未隐藏。

为什么要设置animationDelegate?您不需要在带有完成处理程序块的新动画中使用此功能。Id删除这些和animationDidStop方法,并直接从第一个中的完成句柄块调用[self-moveRight:etc:]

能否定义最小设置以再现错误基本上,我有一个AVCaptureManager,负责显示后摄像头的实时提要。当用户想要拍照时,只需按下屏幕上的按钮,它就会调用如上所示的
takeSnapshots
方法。这很有效,谢谢!但是有一个问题,为什么设置animationDelegate会阻止我的动画发生?我认为animationDelegate从来没有被调用过,因为您使用过一种新的动画方法(带有块),非常确定didstop等不是这些方法的一部分(不再需要),所以moveRight:从未被调用过Dok,知道了。我是UIView动画的新手,所以我在raywenderlich.com上学习了教程,但我想从那时起事情已经改变了。谢谢你的回复!