Konrad Wright
Konrad Wright

Reputation: 1264

Fade background content to new background

I want to be able to fade my background from my image bg1 to bg2.

Right now I'm trying to animate it by...

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];

However I get this error...

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

It says that scene.background.contents in Apple's API, but I can't figure out how to animate it.

Upvotes: 0

Views: 264

Answers (2)

Minestrone-Soup
Minestrone-Soup

Reputation: 399

Here's an answer if you wish to use the UIImageView solution.

Set the image of bg1 and bg2:

//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];

Change opacity:

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

Pretty straight forward from there. To call the methods at delayed times use:

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

Upvotes: 2

Konrad Wright
Konrad Wright

Reputation: 1264

As far as I can tell, there is no way to animate a SCNScene's background's contents property.

However you can make the SCNView's background a clear color, and remove any contents from scene.background.contents then structure your view hierarchy to look like this.

UIView
|
|_UIImageView *backgroundOne
|
|_UIImageView *backgroundTwo
|
|_SCNView *gameView

After that you can animate the UIImageViews as needed with

[UIView animateWithDuration:time animations:animation_block];

Upvotes: 0

Related Questions