4

我正在尝试使用CATransition. 我在处理两个的方法中执行此操作UIViews,一个用于背景,另一个用于前景。动画UIView是前景。但是我不希望新内容替换旧内容,因此我将所有旧内容从前台移动到后台UIView,期望它只会为UIView从一侧的推送过渡设置动画,而是它取代了旧的UIView. 有什么办法可以做我想做的事情,比如UIViews从前景和背景UIViews具有正确内容的那一刻起锁定状态?

我的代码是这样的:

UIView *newView = argView; // argView is the view passed as a parameter to the method
// move views from foreground to background
for (UIView *v in [fgView subviews]) {
    [v removeFromSuperview];
    [bgView addSubview:v];
}

// Slide animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[fgView.layer addAnimation:transition forKey:nil];
[fgView addSubview:newView];

[编辑] 我一直在做一些研究。显然(或我认为可能发生的事情)是背景的 UIViews 删除正在采用隐式动画。有没有办法阻止它动画?

[编辑] 答:是的,可以做到(禁用隐式动画),它是通过以下代码完成的

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
for (UIView *v in [fgView subviews]) {
    [v removeFromSuperview];
    [bgView addSubview:v];
}
[CATransaction commit];

这就是我所做的,它应该可以工作(根据苹果的开发者门户网站)但它没有,所以问题仍然存在。

[编辑] 经过大量测试和阅读后,我发现问题在于,当我从其父视图中删除视图时,Transition 或 Animation 直到稍后才识别此更改,因此它响应最新更改已过时。有什么方法可以强制更新或检查 a removeFromSuperviewor何时addSubview完成?

[编辑] 我真正需要的是: 1) UIViews 的当前状态是:bgView 带有阴影的 UIView 或 midView 后面的旧视图(这个视图不应该是动画的)。一个从不动画的 midView。带有当前 UIView 的 fgView。2)我想将 fgView 的内容移动到 bgView (不会被动画化)并动画化添加到 fgView 的新 UIView 的外观(使用幻灯片、淡入/淡出或翻转动画)。

4

2 回答 2

5

也许更好的方法是对旧内容进行“截图”并将截图添加为 UIImageView,然后您可以将 UIView 的内容更改为新内容。

之后,您可以轻松地为两个视图添加动画,无需担心旧内容被更改。

您可以将以下两个文件添加到您的项目中,并通过调用获取 UIView 的屏幕截图UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithUIView:view]];

因为它是一个 UIImageView,所以你不必担心视图的内容会被改变。

UIImage+ImagewithUIView.h

@interface UIImage (UIImage+ImagewithUIView)
+ (UIImage *)imageWithUIView:(UIView *)view;
@end

UIImage+ImagewithUIView.m

@implementation UIImage (UIImage+ImagewithUIView)
#pragma mark -
#pragma mark TakeScreenShot
static CGContextRef createBitmapContext(int pixelsWide, int pixelsHigh)
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGContextRef bitmapContext = CGBitmapContextCreate(nil, pixelsWide, pixelsHigh, 8, 0, colorSpace, bitmapInfo);
    CGColorSpaceRelease(colorSpace);

    return bitmapContext;
}

+ (UIImage *)imageWithUIView:(UIView *)view
{
    CGSize screenShotSize = view.bounds.size;
    CGContextRef contextRef = createBitmapContext(screenShotSize.width, screenShotSize.height);
    CGContextTranslateCTM (contextRef, 0, screenShotSize.height);
    CGContextScaleCTM(contextRef, 1, -1);

    [view.layer renderInContext:contextRef];
    CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
    CGContextRelease(contextRef);

    UIImage *img = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    // return the image
    return img;
}
@end
于 2011-08-29T17:00:45.910 回答
3
UIView *newView = argView; // argView is the view passed as a parameter to the method
// move views from foreground to background
[fgView.layer removeAllAnimations]; // make sure all, if any, animations are removed.
for (UIView *v in [fgView subviews]) 
{
    // [v removeFromSuperview]; // this should not be needed addSubview does it for you
    [bgView addSubview:v];
}

// Slide animation
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
transition.removedOnCompletion = YES; // force removal of animation when completed.
[fgView addSubview:newView];
[fgView.layer addAnimation:transition forKey:nil];
于 2011-08-29T22:22:15.350 回答