我正在开发一个 iOS 相机应用程序,并希望创建一个类似于默认相机应用程序在捕获图像时所做的动画(而不是快门动画)。捕获的图像似乎落入位于屏幕左下角的照片查看器中。关于如何实现这一目标的任何想法?我以前没有尝试过动画视图控制器,所以我对如何做有点迷茫。
* 编辑实现问题 * 所以我已经学会了如何制作动画,并且动画似乎可以与测试图像一起正常工作,但是当我使用从 AVCapture 连接返回的实际图像时,动画会变得混乱。图像看起来失真(纵横比有问题)并且它遵循的路径与实际路径相反。这是我用来制作动画的代码:
- (void)animateImage:(UIImage*) image {
UIImageView *animator = [[UIImageView alloc]initWithImage:image];
animator.frame = previewView.frame;
animator.contentMode = UIViewContentModeScaleAspectFit;
[self.previewView addSubview:animator];
[CATransaction begin]; {
[CATransaction setCompletionBlock:^{
[animator removeFromSuperview];
}];
// Set up scaling
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
// Set to value to (0,0) rectangle
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(0,0)]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;
// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//Setting Endpoint of the animation
CGPoint endPoint = CGPointMake( animator.frame.size.width, animator.frame.size.height);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 0, 0);
// CGPathMoveToPoint(curvedPath, NULL, animator.frame.size.width/2, animator.frame.size.height/2);
// CGPathAddCurveToPoint(curvedPath, NULL, 0,0,0,0, endPoint.x, endPoint.y);
CGPathAddLineToPoint(curvedPath, NULL, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
CAAnimationGroup *group = [CAAnimationGroup animation];
// group.fillMode = kCAFillModeForwards; // keep the final value after completion of animation
// group.removedOnCompletion = NO; // "
[group setAnimations:[NSArray arrayWithObjects: pathAnimation, resizeAnimation, nil]];
group.duration = 3.0f;
group.delegate = self;
[group setValue:animator forKey:@"imageViewBeingAnimated"];
[animator.layer addAnimation:group forKey:@"cameraAnimation"];
} [CATransaction commit];
}
当我以横向模式拍照时,它似乎可以工作,但不能在 Portrait 模式下。这与方向有关,但我无法弄清楚可能出了什么问题..
“previewView”框架是 AVCapture 预览层,我在其上添加 UIImage,然后缩放并将其移动到角落。
* 最终编辑 * 修正了使用变换属性而不是 bounds.size。与此处的示例类似: http ://www.verious.com/article/animating-interfaces-with-core-animation-part-3/ 呼!