How can I set Background image on MyScene in ios7 - objective-c

When I am setting a background image on a SKScene. I have written the code below:
#import "JTEDMyScene.h"
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#interface MyScene()
#property BOOL isContentCreated;
#end
#implementation MyScene
-(void) didMoveToView:(SKView *)view
{
if(!self.isContentCreated)
{
self.isContentCreated = YES;
[self createSceneContent];
}
}
-(void)createSceneContent
{
[self addChild:[self createBackground]];
}
-(SKSpriteNode *)createBackground
{
SKSpriteNode *backgroundSprite;
SKTexture *textureImage;
if (IS_IPAD) {
textureImage = [SKTexture textureWithImageNamed:#"home_ipad1.jpg"];
backgroundSprite = [SKSpriteNode spriteNodeWithTexture:textureImage];
}
backgroundSprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
return backgroundSprite;
}
Device orientation modes are only Landscape left and landscape right. My image size is width = 1024 and height= 768 and 264 dpi. But the image is not showing fully a portion of image is showing.

Maq has the right idea. I think what he's saying is, the problem lies in your view controller .m file, instead of in your Scene. Instead of creating your SKView and SKScene and presenting the SKScene from within the viewDidLoad method of your VC, put that code in the viewWillLayoutSubviews method instead. Something along the lines of this:
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
SKView * skView = (SKView *)self.view;
SKScene * scene = [YourScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
Apparently, viewWillLayoutSubviews gets called later than viewDidLoad. When viewDidLoad is called, the view hasn't even been added to the view hierarchy yet.
Ray Wenderlich's Sprite Kit Tutorial for Beginners quotes this answer from SO as to why this happens.

Try to present and create MyScene in viewWillLayoutSubviews - if you haven't done it already.
It may work if you just rename viewDidLoad to viewWillLayoutSubviews and rename the super call too.

I started a blank project and copy-pasted your code into it and it works fine. I know you said you set your device orientation, but did you do that in your project settings? That's the only problem I can think of. Select your project, general tab, and under Deployment Info, there's a "Devices" drop-down box. Select iPad and make sure "Landscape Left" and "Landscape Right" are the only two checked. My guess is that the project thinks it's supposed to be starting in portrait mode, so it's stretching your 768-tall image vertically to 1024 at start; then when your app determines that it's in landscape orientation, it just rotates that stretched image accordingly, which would result in the image being cut off.

yes, to control and resize background image (in my case) correspond to device orientation, you have to use -(void)viewWillLayoutSubviews
but don't forget, it will start from first scene by default, so you need to use some "sceneNumber" selector in this function to point to actual scene

Related

SpriteKit: Presenting the initial scene wth a transition

I've created multiple transitions from between my game scenes, so I'm unsure why this code is not working.
In my GameViewController, I am simply setting up my TitleScene to appear as the next scene, but I would like it to fade in. I've done this in my touchesBegan succesfully.
Here is the code:
#implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
SKScene * scene = [TitleScene sceneWithSize:skView.frame.size];
scene.scaleMode = SKSceneScaleModeResizeFill;
// Present the scene.
SKTransition *transition = [SKTransition fadeWithColor:[SKColor blackColor] duration:3.6];
[skView presentScene:scene transition:transition];
}
You can't use SpriteKit to present the initial scene with a transition because the SKView is (by definition) not showing any scene before the initial scene, so from its perspective there's nothing for it to transition from.
Presumably, from your point of view, you want to fade into your scene from whatever the user is seeing before SpriteKit gets started — the launch screen of your app, maybe? A common strategy for that is to have the initial storyboard scene contain a UIImageView containing the same image as your launch screen, layered on top of your SKView. When you're ready to start the game, use UIView animateWithDuration:animations:completion: to fade out the image view and remove it from the view hierarchy, and your SKView will be revealed beneath.

Custom UIProgressView in iOS7 not possible?

I know this was already asked about a hundred times here, but I couldn´t find a suitable answer in the other questions.
My problem is the height of my UIProgressView. While everything worked as expected in iOS6, now in iOS7 nothing goes right.
I tried the following:
1.Setting the custom layout in the drawRect-Method:
Works like a charm in iOS6, but in iOS7 the progress is set to 100% from the beginning or the bar is very thin.
2.Setting the layout with the progressImage and trackImage property of the UIProgressView appearance
Also not working under iOS7. Here the bar progress is set to 100% from the beginning, too. Some people write that it should be possible this way, but I can not confirm that for iOS7.
3.Using initWithProgressStyle for initialization and then setting the frame of the progress view
Not working for me under iOS6 and iOS7. In iOS7 the bars are just very slim.
For me right now it is pretty frustrating because the bars are either at 100% or they are mega-slim. Can anyone give me a suggestion to reach the old layout of my progress views. I think it has to be possible because if I look at my Spotify app on the iPhone (iOS7 installed), the progress view looks like before.
Thank you very much!
Well, the problem is seams that iOS6 UIProgressView and iOS7 UIProgressView have different internal subviews structure. iOS6 progress view is a single view without child view (or some minor view), iOS7 progress view have few additional subview for drawing progress bar and background.
If you remove all subview of UIProgressView on iOS7 than you drawRect: method will work the same as before on iOS6, but you will be totally responsible about drawing your progress view content including progress bar and background.
- (id) initWithCoder: (NSCoder*)aDecoder
{
if(self=[super initWithCoder: aDecoder])
{
// Also you can setup height of your progress here
// self.frame = CGRectMake(0,0,100,yourHeight);
NSArray *subViews = self.subviews;
for(UIView *view in subViews)
{
[view removeFromSuperview];
}
}
return self;
}
I made kind of a workaround for this problem. I hope somebody can give a nice answer for a normal UIProgressView though.
I wrote a UIView subclass with round corners and a view inside of it which changes its size depending on the given progress. I only use colors for the background, but images would be possible too. Here´s the code:
#import "CustomProgressView.h"
#import <QuartzCore/QuartzCore.h>
#interface CustomProgressView ()
#property (nonatomic, retain) UIView *progressView;
#end
#implementation CustomProgressView
#synthesize progressColor,trackColor,progressView,progress;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.cornerRadius = 5;
// clipsToBounds is important to stop the progressView from covering the original view and its round corners
self.clipsToBounds = YES;
self.progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
[self addSubview:self.progressView];
}
return self;
}
-(void)setProgressColor:(UIColor *)theProgressColor {
self.progressView.backgroundColor = theProgressColor;
progressColor = theProgressColor;
}
-(void)setTrackColor:(UIColor *)theTrackColor {
self.backgroundColor = theTrackColor;
trackColor = theTrackColor;
}
-(void)setProgress:(float)theProgress {
progress = theProgress;
CGRect theFrame = self.progressView.frame;
theFrame.size.width = self.frame.size.width * theProgress;
self.progressView.frame = theFrame;
}
#end
I had a custom UIProgressView with it's own drawRect being updated from a background process.
In iOS6 all was working while in iOS7 the progressbar just did not update.
I have added layoutSublayersOfLayer right after the setProgress like this
[self.loadingProgress setProgress:pv.floatValue];
[self.loadingProgress layoutSublayersOfLayer:self.loadingProgress.layer];
and it worked like a charm.
I hope this helps someone.
Avoid a headache and use this excellent library:
YLProgressBar
Copy YLProgressBar.h and YLProgressBar.m from the YLProgressBar folder.
#import "YLProgressBar.h" in the file(s) you want to use the progress bar
Add your progress bar either by code or by xib
progressBar.type = YLProgressBarTypeRounded;
progressBar.progressTintColor = [UIColor greenColor];
progressBar.stripesOrientation = YLProgressBarStripesOrientationVertical;
progressBar.stripesDirection = YLProgressBarStripesDirectionLeft;
You have a nice, fully functional progress bar that supports width, height, modifications.

Squashed and distorted view after device rotation

I've been reading through theses pages for a while now and have been having some trouble getting my CGRect to load properly. I learned it was because I'm forcing an orientation after viewDidLoad runs, and I'm trying to get the CGRect to draw again after. Basically, this is what I'm looking at:
-(void)viewDidLoad
{
//mybutton.frame = CGRectMake(...);
//...
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
When the view appears, the CGRect is very squashed and distorted, due to the rotation of the view.
You could extract the drawing code out from viewDidLoad and into its own function. Then you can call the function from within the shouldAutorotateToInterfaceOrientation and from the viewDidLoad.

How do I lock my background image in place when rotating device?

I'd like to "lock" my background image (a UIImageView) such that it doesn't look like it's getting cropped when I rotate my device. It looks correct in landscape view, but doesn't follow along with the rotation.
I've tried stretching it using setAutoresizingMask, but the results look horrible. I've fiddled around with the transform property of the UIImageView, but fail to make it look correct.
Any pointers would be appreciated. I'm not even sure if I'm on the right track.
You can have different images for portrait/landscape orientations and switch between the two with UIView animations on willRotate callbacks. I tried it and the result is quite smooth.
OK, I managed to make it work using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
...
UIColor *color = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.png"]];
self.background.backgroundColor = color;
[color release];
...
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
float angle = (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) ? M_PI_2 : 0.0;
self.background.transform = CGAffineTransformMakeRotation(angle);
}
Bear in mind that background~ipad.png is 1024x768, that is landscape as default. If your image is portrait you need to check for landscape instead of portrait in the conditional cases.

iPad - xib ignoring orientation on view loads

iPad question:
We have views that we're initializing from .xibs, with each .xib containing both landscape and portrait layouts.
The first view we load respects the orientation of the device, but any subsequent views seem to have confusion over which orientation they should be using. Or, rather, they seem to ignore orientation altogether and go with whichever orientation the .xib file was saved with. (i.e. We saved a .xib file in landscape, and even though the device was held in portrait position, it loaded the view from the .xib with a landscape layout.)
This is how we're calling in these views:
Settings *settingsTEMP = [[Settings alloc] init];
self.settings = settingsTEMP;
[self.view insertSubview:settings.view atIndex:0];
[settingsTEMP release];
The first of the calls (the one that respects the device's orientation) is in the viewDidLoad. The second, which uses a different view (the one that doesn't respect the device's orientation) is in a method that's called after a button is pressed, but uses the same syntax.
If we put both into viewDidLoad, they both respect the orientation.
We've tried searching for anyone having similar issues but have been thus far unsuccessful. Thanks in advance for any help you can provide.
I use the following
UIInterfaceOrientation toInterfaceOrientation = self.interfaceOrientation;
if ((toInterfaceOrientation == UIDeviceOrientationLandscapeLeft) ||
(toInterfaceOrientation == UIDeviceOrientationLandscapeRight))
{
NSLog(#"vSettings UIDeviceOrientationLandscape");
CGRect contentRect = CGRectMake(0,0,1024,768);
vSettings.bounds = contentRect;
CGRect myFrame = vSettings.frame;
myFrame.origin.x = 0.0;
myFrame.origin.y = 0.0;
vSettings.frame = myFrame;
UIImage *image;
image = [UIImage imageNamed: #"Default-Landscape.png"];
[backGroundSettings setImage:image];
}
I hate having to do this as it seems a right fudge but it works for me. Called just after I create the view.