淡化背景内容到新的背景

时间:2015-04-16 18:17:07

标签: objective-c xcode swift scenekit

我希望能够将我的背景从我的图片bg1淡化为bg2

现在我正试图通过......来动画它。

scene.background.contents = @"bg1";
[CATransaction begin];
CABasicAnimation *displayBackground2 =
  [CABasicAnimation animation];
displayBackground2.keyPath = @"contents";
displayBackground2.toValue = @"bg2";
displayBackground2.duration = 5.0;
[scene.background addAnimation:displayBackground2
                        forKey:@"contents"];
[CATransaction commit];

但是我收到了这个错误......

[SCNKit ERROR] contents is not an animatable path (from <SCNMaterialProperty: 0x170149530 | contents=bg1>)

它在Apple的API中说scene.background.contents,但我无法弄清楚如何为它制作动画。

2 个答案:

答案 0 :(得分:2)

如果您希望使用UIImageView解决方案,请在此处回答。

设置bg1bg2的图像:

//Declare them in header file

CGFloat imageHeight = self.height
CGFloat proportionalWidth = (height of background image / imageHeight) * self.width

//Set height and width to value of height and width of screen respectively

self.bg1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageHeight, proportionalWidth)];
[self.bg1 setImage:[UIImage imageNamed:@"the image"]];;
[self.view addSubview:self.bg1];

更改不透明度:

[UIView animateWithDuration: duration
                 animations: ^{[self.bg1 setAlpha: 0]}
];

从那里很直接。要在延迟时间调用方法,请使用:

[self performSelector:@selector(methodName) withObject:nil afterDelay:delay];

答案 1 :(得分:0)

据我所知,没有办法为SCNScene background的{​​{1}}财产制作动画。

但是,您可以将contents的背景设为一个清晰的颜色,并从SCNView中删除所有内容,然后将您的视图层次结构看起来像这样。

scene.background.contents

之后,您可以根据需要使用

UIView | |_UIImageView *backgroundOne | |_UIImageView *backgroundTwo | |_SCNView *gameView 设置动画
UIImageView