【问题标题】:How can I use a video as custom view controller transition in iOS?如何在 iOS 中使用视频作为自定义视图控制器转换?
【发布时间】:2015-11-09 15:33:06
【问题描述】:
【问题讨论】:
标签:
ios
video
uiviewcontroller
gpuimage
uiviewanimationtransition
【解决方案1】:
我不知道我可以在实现UIViewControllerAnimatedTransitioning 的类中将UIViews 添加到transitionContext 中的containerView。
所以我在过渡开始时将filterView 添加到容器中,并在完成块中再次将其删除。
@interface VideoTransitionAnimator() {
GPUImageOutput<GPUImageInput> *filter;
}
@property(nonatomic, strong) GPUImageMovie * movieFile;
@property(nonatomic, strong) GPUImagePicture * sourcePicture;
@end
@implementation VideoTransitionAnimator
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 2;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
ViewController* fromViewController = (ViewController *) [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 0;
NSURL *sampleURL = [[NSBundle mainBundle] URLForResource:@"sample.m4v" withExtension:@"mp4"];
self.movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
self.movieFile.shouldRepeat = NO;
filter = [[GPUImageChromaKeyTransparencyFilter alloc] init];
[(GPUImageChromaKeyTransparencyFilter *)filter setColorToReplaceRed:0.0 green:1.0 blue:0.0];
[(GPUImageChromaKeyTransparencyFilter *)filter setThresholdSensitivity:0.4];
[self.movieFile addTarget:filter];
GPUImageView *filterView = [[GPUImageView alloc] init];
filterView.frame = fromViewController.view.frame;
filterView.backgroundColor = [UIColor clearColor];
[filter addTarget:filterView];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:filterView];
//rfilterView.fillMode = kGPUImageFillModeStretch;
[self.movieFile startProcessing];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.alpha = 1;
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
[filterView removeFromSuperview];
}];
}
@end