//src是原UIViewController,dst是目标UIViewController, 目的是使用presentModalViewController方法从src中弹出dst,实现如下 dst.view.backgroundColor = [UIColor clearColor]; controller.modalPresentationStyle = UIModalPresentationCurrentContext; [controller presentModalViewController:dst animated:YES];
//方法说明:根据提供的位置和范围,将屏幕图像生成为UIImage并放回 //参数1 aView : 待剪切的原始UIView //参数2 rect : 剪切范围 -(UIImage *)CropImageFrom:(UIView *)aView inRect:(CGRect)rect { CGSize cropImageSize = rect.size; UIGraphicsBeginImageContext(cropImageSize); CGContextRef resizedContext = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(resizedContext, -(rect.origin.x), -(rect.origin.y)); [aView.layer renderInContext:resizedContext]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
//原视图为OriginalView,新视图为NewView -(void)SwitchFromOriginalView:(UIView *)OriginalView ToNewView:(UIView *)NewView{ [NewView setAlpha:0.01f]; [NewView setFrame:CGRectMake(320, NewView.frame.origin.y, NewView.frame.size.width, NewView.frame.size.height)]; [UIView beginAnimations:@"DisplayNewView" context:nil]; //[UIView setAnimationDidStopSelector:@selector()]; //动画完成后执行指定方法 [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:1.5]; [NewView setFrame:CGRectMake(0, NewView.frame.origin.y, NewView.frame.size.width, NewView.frame.size.height)]; [OriginalView setFrame:CGRectMake(-320, OriginalView.frame.origin.y, OriginalView.frame.size.width, OriginalView.frame.size.height)]; [NewView setAlpha:1.0f]; [UIView commitAnimations]; }
//结合拷屏功能,可将任意的UIView生成一个CALayer,从而应用Core Animation的一些效果。特别是在UIView较为复杂的情况下,直接应用动画效果会有性能问题,而针对CALayer做完动画效果后,再把真正的UIView显示出来,是一种提高性能的方式。 CALayer *layer = [CALayer layer]; [layer setFrame:CGRectMake(0, 80, 320, 320)]; //指定显示位置 layer.contents = (__bridge id) [image CGImage]; //载入图像 [src.view.layer addSublayer:layer]; //显示在src的UIViewController的视图上
//当顶层视图不是全屏状态(或具有透明背景),同时要相应顶层视图的按钮操作和底层视图的按钮操作时,可以在加载顶层视图的时候,通过setFrame方法限制顶层视图的范围,从而解决这一问题: [dst.view setFrame:CGRectMake(0, 80, 320, 320)]; //顶层视图的显示位置 [src.view addSubview:dst.view]; //如果当顶层视图的显示范围不好改变时,可以通过传递相应事件的方式来解决问题,即实现touchBegin等方法