两个UIViews之间的自定义转换

lef*_*pin 6 iphone core-animation uiview

我有两个视图"A"和"B".在窗口中间浮动(它不是全屏).B,将取代A的视图是全屏.我想编写一个自定义过渡,翻转A以显示背面的B,但同时缩放翻转矩形,以便在翻转完成时,B为全屏.

toView:持续时间:选择:我已经使用可用transitionFromView翻转过渡试图完成,但我只能把它翻转而不是以A开头的框架翻转,并与B的帧结束了整个屏幕.

我知道我可以对视图的图层进行类似3D的转换,但我不确定应该使用哪组动画API来完成此操作.有一两件事我想是在transitionWithView的动画块修改视图的图层属性:持续时间:选择:动画:完成:但没有工作,因为它似乎只兑现视图属性修改.

有人能指出我正确的方向吗?我非常感激.

更新:到目前为止,这是我的代码.您可以在此处观看视频:http://www.youtube.com/watch?v = -xNMD2fGRwg

CGRect frame = [[UIApplication easybookDelegate] rootViewController].view.bounds ;
float statusHeight = [UIApplication sharedApplication].statusBarFrame.size.height ;
frame.origin.y = statusHeight ;
frame.size.height -= statusHeight ;
self.view.frame = frame ; // self.view is view "B"

// Put the snapshot as thet topmost view of our view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ; // image is a snapshot of view "A"
imageView.frame = self.view.bounds ;
[self.view addSubview:imageView] ;
[self.view bringSubviewToFront:imageView] ;

// Pre-transform our view (s=target/current, d=target-current)
CGAffineTransform transform = CGAffineTransformIdentity ;

// Translate our view
CGPoint center  = CGPointMake(screenOrigin.x + (image.size.width/2.0), screenOrigin.y + (image.size.height/2.0)) ;
float dX = center.x - self.view.center.x ;
float dY = center.y - self.view.center.y ;
NSLog( @"dx: %f, dy: %f" , dX, dY ) ;

transform = CGAffineTransformTranslate(transform, dX, dY) ;

// Scale our view
float scaleWFactor = image.size.width / self.view.frame.size.width ;
float scaleHFactor = image.size.height / self.view.frame.size.height ;
transform = CGAffineTransformScale(transform,scaleWFactor, scaleHFactor) ;

self.view.transform = transform ;

[[[UIApplication easybookDelegate] rootViewController].view addSubview:self.view] ;

// Perform the animation later since implicit animations don't seem to work due to
// view "B" just being added above and hasn't had a chance to become visible. Right now
// this is just on a timer for debugging purposes. It'll probably be moved elsewhere, probably
// to view "B"'s -didMoveToSuperview
double delayInSeconds = 0.3;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [UIView transitionWithView:self.view duration:3 options:UIViewAnimationOptionTransitionFlipFromRight| UIViewAnimationOptionCurveEaseOut animations:^(void) 
        {
        [imageView removeFromSuperview] ;
        self.view.transform = CGAffineTransformIdentity ;
        } 
        completion:^(BOOL finished){
                [self.view removeFromSuperview] ;
                [navController presentModalViewController:self animated:NO] ;
        }] ;
});

[imageView release];
Run Code Online (Sandbox Code Playgroud)

Cha*_*zak 3

我以前做过这个。这是我的做法的要点:

  1. 视图 A 在屏幕上
  2. 创建视图 B,给它一个使其全屏的框架,但还不将其添加到屏幕
  3. 视图 B 有一个 UIImageView 作为最顶层的视图
  4. 捕获View A的UIImage,并将其设置为View B的图像视图的图像

    UIGraphicsBeginImageContext(view.bounds.size); 
    [viewA.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    viewB.imageView.image = image;
    
    Run Code Online (Sandbox Code Playgroud)
  5. 设置视图 B 的变换(缩放和平移),使其缩小为视图 A 的大小并位于视图 A 在屏幕上的位置

  6. 将视图 B 添加到屏幕
  7. 此时,用户还没有注意到任何变化,因为视图 ​​B 看起来与视图 A 完全相同
  8. 现在启动一个动画块,您可以在其中
    • [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:ViewB
    • 将视图 B 的变换设置为 CGAffineTransformIdentity
    • 从视图 B 中删除视图 B 的 imageview

结果是一个动画,看起来像视图 A 正在翻转并缩放以填充屏幕,而视图 B 作为另一侧。