I have been struggling with this for days, for some reason my SKScenes are not deallocating correctly, this results in bounded memory growth as each time i exit and enter a scene the memory jumps up. This means after say 10 rounds of the game the App crashes. As far as i'm aware after much checking i do not have any retain cycles or strong references to the scenes themselves and whilst i know textures are cached and held in memory surely once preloaded the memory shouldn't be going up each time.
This is how i setup the skview and first scene in the viewcontroller:
-(void)loadStartScreen{
SKView *theView = (SKView *) self.view;
theView.showsFPS = YES;
theView.showsNodeCount = YES;
//Sprite Kit applies additional optimizations to improve rendering performance
theView.ignoresSiblingOrder = YES;
// Create and configure the scene.
MainMenuScene *theScene = [MainMenuScene sceneWithSize:theView.bounds.size];
theScene.scaleMode = SKSceneScaleModeAspectFill;
theScene.backgroundColor = [UIColor grayColor];
// Present the scene
[theView presentScene:theScene];
This is the code from within my MainMenuScene to create and move to the next scene:
SKScene *theScene;
SKTransition *theTransition;
switch (theTag.intValue) {
case 0: // start game
// stop music
[[appDelegate musicPlayer]stop];
theScene = [[GameLevelScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition fadeWithDuration:1.0];
break;
case 1: // settings
theScene = [[SettingsScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 2: // iap
theScene = [[IAPScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 3: // unlocks screen
NSLog(#"scene is %#",self.view.scene);
theScene = [[deletmet alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
NSLog(#"scene is after %#",self.view.scene);
break;
case 4: // level complete
theScene = [[LevelCompleteScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 5: // cheats
theScene = [[CheatsScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
default:
break;
}
Now i know that you should use "[self.view presentScene:]" as the SkView holds the scene but because nothing is working i've been experimenting to see why the scene does not seem to deallocate. When i try pressing the button for case 2 following i get the resulting NSlogs:
[self presentScene:theScene];
NSLOG result = "IAP SCENE DEALLOC" but the user is not taken to the next scene.
[self presentScene:nil];
NSLOG result = "IAP SCENE DEALLOC" but the user is not taken to the next scene.
[self.view presentScene:theScene];
No nslog results even though the current MainMenuScene has an NSLOG for dealloc
[self.view presentScene:nil];
NSLOG result = IAP SCENE DEALLOC and the screen becomes grey
Now this is all very odd as surely when i do get a DEALLOC NSLOG message it should be giving me the NSLOG for the dealloc of the current scene aka MainMenuScene rather than the scene it should be loading.
Am i doing something wrong? Am i working with the SKView incorrectly, any help would be greatly appreciated as i have read a variety of posts all of which have not helped.
Update: This is the code for each scene that i am moving back and forth between, notice the lack of actions or any strong references to self:
Main Menu:
#import "MainMenuScene.h"
#import "SKScene+SceneUtils.h"
#import "GameLevelScene.h"
#import "SettingsScene.h"
#import "CreditsScene.h"
#import "IAPScene.h"
#import "UnlocksScene.h"
#import "LevelCompleteScene.h"
#import "CheatsScene.h"
#import "AppDelegate.h"
#import "UserDetails.h"
#import "ItemBannerLabel.h"
#import "ItemWindow.h"
#import "TextureList.h"
#import "TextureLoader.h"
#interface MainMenuScene(){
SKSpriteNode *backgroundImage;
SKSpriteNode *topBar;
SKSpriteNode *bottomBar;
SKSpriteNode *ladybirds;
SKLabelNode *title;
SKLabelNode *subtitle;
SKLabelNode *coffee;
ItemWindow *settingsWin;
ItemWindow *iapWin;
ItemWindow *unlocksWin;
AGSpriteButton *startButton;
AGSpriteButton *continueButton;
AGSpriteButton *settingsButton;
AGSpriteButton *iapButton;
AGSpriteButton *unlocksButton;
SKEmitterNode *theParticles;
//SKAction *delay;
//AppDelegate *appDelegate;
}
#end
#implementation MainMenuScene
#pragma mark - Scene Appears
-(void)didMoveToView:(SKView *)view {
// setup UI
[self createUI];
// setup view
[self setupView];
}
-(void)willMoveFromView:(SKView *)view{
}
#pragma mark - CreateUI
-(void)createUI{
// scene size
self.scene.size = [[TextureList sharedManager]returnTextureSize:#"kMMBg"];
// background
self.scene.backgroundColor = [SKColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
// masked background
backgroundImage = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:kMMBg] size:[[TextureList sharedManager]returnTextureSize:#"kMMBg"]];
backgroundImage.position = screenCenter;
backgroundImage.zPosition = self.zPosition+1;
[self addChild:backgroundImage];
ladybirds = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:kMMLadybirds] size:[[TextureList sharedManager]returnTextureSize:kMMLadybirds]];
ladybirds.position = CGPointMake(CGRectGetMaxX(self.frame)-ladybirds.frame.size.width/2, screenCenter.y);
ladybirds.zPosition = bottomBar.zPosition+5;
[self addChild:ladybirds];
// buttons
startButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMStartBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMStartBtn]];
[startButton setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
startButton.position = CGPointMake(ladybirds.position.x-ladybirds.frame.size.width/4, ladybirds.position.y-ladybirds.frame.size.height/16);
startButton.zPosition = ladybirds.zPosition+1;
[startButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:startButton];
// emitter
theParticles = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"FlowerPetalParticle" ofType:#"sks"]];
theParticles.zPosition = backgroundImage.zPosition+1;
theParticles.position = CGPointMake((CGRectGetMinX(self.scene.frame)),startButton.position.y);
theParticles.particlePositionRange = CGVectorMake(0.0, CGRectGetMaxY(self.frame)-topBar.frame.size.height-bottomBar.frame.size.height);
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"ipad"]) {
theParticles.particleLifetime = 8.0;
theParticles.particleScale = 0.5;
theParticles.particleScaleRange = 0.2;
}
[self addChild:theParticles];
topBar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:[[TextureList sharedManager]returnTextureSize:kMMTopBar]];
topBar.position = CGPointMake(screenCenter.x, CGRectGetMaxY(self.frame)-topBar.frame.size.height/2);
topBar.zPosition = theParticles.zPosition+1;
[self addChild:topBar];
bottomBar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:[[TextureList sharedManager]returnTextureSize:kMMBottomBar]];
bottomBar.position = CGPointMake(screenCenter.x, CGRectGetMinY(self.frame)+bottomBar.frame.size.height/2);
bottomBar.zPosition = theParticles.zPosition+1;
[self addChild:bottomBar];
settingsWin = [[ItemWindow alloc]initWithImageNamed:kMMCreditsBtn withLabel:#"SETTINGS" setLabelTop:NO];
settingsWin.theLabel.fontColor = [UIColor blackColor];
settingsWin.theLabel.fontSize = 15;
settingsWin.position = CGPointMake(CGRectGetMinX(self.frame)+settingsWin.frame.size.width/2+20, CGRectGetMinY(self.frame)+settingsWin.frame.size.height/2+25);
settingsWin.zPosition = bottomBar.zPosition+1;
[self addChild:settingsWin];
settingsButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(settingsWin.size.width*2, settingsWin.size.height*2)];
[settingsButton setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
settingsButton.position = settingsWin.position;
settingsButton.zPosition = settingsWin.zPosition+1;
[settingsButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:settingsButton];
iapWin = [[ItemWindow alloc]initWithImageNamed:kMMIapBtn withLabel:#"SHOP" setLabelTop:NO];
iapWin.theLabel.fontColor = [UIColor blackColor];
iapWin.theLabel.fontSize = 15;
iapWin.position = CGPointMake(settingsWin.position.x+iapWin.frame.size.width+30, settingsWin.position.y);
iapWin.zPosition = bottomBar.zPosition+1;
[self addChild:iapWin];
iapButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(iapWin.size.width*2, iapWin.size.height*2)];
[iapButton setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
iapButton.position = iapWin.position;
iapButton.zPosition = iapWin.zPosition+1;
[iapButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:iapButton];
unlocksWin = [[ItemWindow alloc]initWithImageNamed:kMMUnlockBtn withLabel:#"UNLOCKS" setLabelTop:NO];
unlocksWin.theLabel.fontColor = [UIColor blackColor];
unlocksWin.theLabel.fontSize = 15;
unlocksWin.position = CGPointMake(iapWin.position.x+unlocksWin.frame.size.width+30, iapWin.position.y);
unlocksWin.zPosition = bottomBar.zPosition+1;
[self addChild:unlocksWin];
unlocksButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(unlocksWin.size.width*2, unlocksWin.size.height*2)];
[unlocksButton setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
unlocksButton.position = unlocksWin.position;
unlocksButton.zPosition = unlocksWin.zPosition+1;
[unlocksButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:3] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:unlocksButton];
// Labels
title = [[SKLabelNode alloc]initWithFontNamed:kFontName];
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"iphone4"]) {
title.fontSize = 60;
}
else{
title.fontSize = 75;
}
title.fontColor = [UIColor blackColor];
title.position = CGPointMake(topBar.frame.size.width/4, topBar.position.y-10);
title.zPosition = topBar.zPosition+1;
title.text = #"FLOWERS";
[self addChild:title];
subtitle = [[SKLabelNode alloc]initWithFontNamed:kFontName];
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"iphone4"]) {
subtitle.fontSize = 24;
}
else{
subtitle.fontSize = 30;
}
subtitle.fontColor = [UIColor grayColor];
subtitle.position = CGPointMake(title.position.x, title.position.y-title.frame.size.height/2-10);
subtitle.zPosition = topBar.zPosition+1;
subtitle.text = #"THE BEAUTIFUL MEADOW";
[self addChild:subtitle];
AGSpriteButton *testButton3 = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMIapBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMIapBtn]];
[testButton3 setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
testButton3.position = CGPointMake(bottomRight.x-testButton3.frame.size.width/1.5, bottomRight.y+settingsButton.frame.size.height/1.5);
testButton3.zPosition = interfaceLayer;
[testButton3 addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:4] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:testButton3];
AGSpriteButton *testButton4 = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMIapBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMIapBtn]];
[testButton4 setLabelWithText:#"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
testButton4.position = CGPointMake(testButton3.position.x-testButton4.frame.size.width, bottomCenter.y+settingsButton.size.height/1.5);
testButton4.zPosition = interfaceLayer;
[testButton4 addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:5] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:testButton4];
if ([[[UserDetails sharedManager]userDevice] isEqualToString:#"ipad"]) {
title.fontSize = 105;
title.position = CGPointMake(topBar.frame.size.width/6+20, topBar.position.y+10);
subtitle.position = CGPointMake(title.position.x, title.position.y-title.frame.size.height/2-10);
subtitle.fontSize = 41;
settingsWin.theLabel.fontSize = 25;
iapWin.theLabel.fontSize = 25;
unlocksWin.theLabel.fontSize = 25;
settingsWin.position = CGPointMake(CGRectGetMinX(self.frame)+settingsWin.frame.size.width/2+20, CGRectGetMinY(self.frame)+settingsWin.frame.size.height+25);
iapWin.position = CGPointMake(settingsWin.position.x+iapWin.frame.size.width+30, settingsWin.position.y);
unlocksWin.position = CGPointMake(iapWin.position.x+unlocksWin.frame.size.width+30, settingsWin.position.y);
}
}
#pragma mark - Setup View
-(void)setupView{
// setup music players slightly quieter music now
/*appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self fadeVolumeIn:[appDelegate musicPlayer] toVolume:0.45];
[[appDelegate soundFxPlayer]setVolume:0.25];
[[appDelegate soundFxPlayer]play];
// no continue if the user has not progressed past level 1
if ([[UserDetails sharedManager]userCurrentLevel] <= 1) {
continueButton.userInteractionEnabled = NO;
continueButton.hidden = YES;
}
else{
continueButton.userInteractionEnabled = YES;
continueButton.hidden = NO;
}*/
}
#pragma mark - Interaction
-(void)buttonPressed:(NSNumber*)theTag{
SKScene *theScene;
SKTransition *theTransition;
switch (theTag.intValue) {
case 0: // start game
// stop music
//[[appDelegate musicPlayer]stop];
theScene = [[GameLevelScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition fadeWithDuration:1.0];
break;
case 1: // settings
theScene = [[SettingsScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 2: // iap
theScene = [[IAPScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 3: // unlocks screen
theScene = [[UnlocksScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 4: // level complete
theScene = [[LevelCompleteScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
case 5: // cheats
theScene = [[CheatsScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
break;
default:
break;
}
// play sound
[self menuButtonPressed];
[self.view presentScene:theScene transition:theTransition];
}
#end
Settings Screen:
#import "SettingsScene.h"
#import "SKScene+SceneUtils.h"
#import "AGSpriteButton.h"
#import "ItemBannerLabel.h"
#import "TextureList.h"
#import "UnlockController.h"
#import "UserDetails.h"
#import "TutorialFlowerTarget.h"
#import "CreditsScene.h"
#interface SettingsScene(){
AGSpriteButton *backButton;
AGSpriteButton *resetButton;
AGSpriteButton *resetTutorialsButton;
AGSpriteButton *creditsButton;
ItemBannerLabel *titleLabel;
SKLabelNode *copyrightLabel;
UIAlertView *resetGameAlert;
UIAlertView *resetTutAlert;
SKScene *theScene;
SKTransition *theTransition;
SKSpriteNode *menuBg;
}
#end
#implementation SettingsScene
#pragma mark - SCENE APPEARS
-(void)didMoveToView:(SKView *)view {
// setup UI
[self createUI];
}
#pragma mark - CREATE UI
-(void)createUI{
// background
self.scene.backgroundColor = [UIColor whiteColor];
menuBg = [[SKSpriteNode alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDBg] color:nil size:[[TextureList sharedManager]returnTextureSize:#"kMDBg"]];
menuBg.position = screenCenter;
menuBg.zPosition = self.zPosition+1;
[self addChild:menuBg];
// labels
titleLabel = [[ItemBannerLabel alloc]initWithBgImageNamed:kMDTitle withLabel:#"GAME SETTINGS" withfont:kFontName withSize:kFontSizeMDTitle];
titleLabel.position = CGPointMake(topLeft.x+titleLabel.frame.size.width/2+10,topLeft.y-titleLabel.frame.size.height/2-10);
titleLabel.zPosition = interfaceLayer;
[self addChild:titleLabel];
copyrightLabel = [SKLabelNode labelNodeWithFontNamed:kFontName];
copyrightLabel.text = [NSString stringWithFormat:#"COPYRIGHT © 2015 RICHARD ACHERKI"];
copyrightLabel.fontSize = kFontSizeMDSmall;
copyrightLabel.fontColor = [UIColor blackColor];
copyrightLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
copyrightLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
copyrightLabel.position = CGPointMake(bottomCenter.x, bottomCenter.y+copyrightLabel.frame.size.height);
copyrightLabel.zPosition = interfaceLayer;
[self addChild:copyrightLabel];
// buttons
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"ipad"]) {
resetTutorialsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
[resetTutorialsButton setLabelWithText:#"RESET TUTORIALS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
resetTutorialsButton.position = CGPointMake(screenCenter.x, titleLabel.position.y-titleLabel.frame.size.height/2-resetTutorialsButton.frame.size.height/2-30);
resetTutorialsButton.zPosition = menuBg.zPosition+1;
[resetTutorialsButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
}
else{
resetTutorialsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
[resetTutorialsButton setLabelWithText:#"RESET TUTORIALS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
resetTutorialsButton.position = CGPointMake(screenCenter.x, titleLabel.position.y-titleLabel.frame.size.height/2-resetTutorialsButton.frame.size.height/2-30);
resetTutorialsButton.zPosition = menuBg.zPosition+1;
[resetTutorialsButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
}
[self addChild:resetTutorialsButton];
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"ipad"]) {
resetButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
[resetButton setLabelWithText:#"RESET GAME" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
resetButton.position = CGPointMake(resetTutorialsButton.position.x,resetTutorialsButton.position.y-resetTutorialsButton.frame.size.height-20);
resetButton.zPosition = menuBg.zPosition+1;
[resetButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
}
else{
resetButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
[resetButton setLabelWithText:#"RESET GAME" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
resetButton.position = CGPointMake(resetTutorialsButton.position.x,resetTutorialsButton.position.y-resetTutorialsButton.frame.size.height-20);
resetButton.zPosition = menuBg.zPosition+1;
[resetButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
}
[self addChild:resetButton];
if ([[[UserDetails sharedManager]userDevice]isEqualToString:#"ipad"]) {
creditsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
[creditsButton setLabelWithText:#"CREDITS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
creditsButton.position = CGPointMake(resetButton.position.x,resetButton.position.y-resetButton.frame.size.height-20);
creditsButton.zPosition = menuBg.zPosition+1;
[creditsButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
}
else{
creditsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
[creditsButton setLabelWithText:#"CREDITS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
creditsButton.position = CGPointMake(resetButton.position.x,resetButton.position.y-resetButton.frame.size.height-20);
creditsButton.zPosition = menuBg.zPosition+1;
[creditsButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
}
[self addChild:creditsButton];
backButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:[[TextureList sharedManager]returnTextureSize:kMDButton]];
[backButton setLabelWithText:#"BACK" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
backButton.position = CGPointMake(bottomRight.x-backButton.frame.size.width/2-10, bottomRight.y+backButton.frame.size.height/2+10);
backButton.zPosition = menuBg.zPosition+1;
[backButton addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:3] forControlEvent:AGButtonControlEventTouchUpInside];
[self addChild:backButton];
}
#pragma mark - BUTTON PRESSED
-(void)buttonPressed:(NSNumber*)theTag{
if (theTag.intValue == 0) { // reset tutorials
resetTutAlert = [[UIAlertView alloc]initWithTitle:#"RESET TUTORIALS" message:#"Are you sure you want to reset tutorials?\nThis will cause all tutorials to show again when playing the game." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[resetTutAlert show];
}
else if (theTag.intValue == 1) { // reset game
resetGameAlert = [[UIAlertView alloc]initWithTitle:#"RESET THE GAME" message:#"Are you sure you want to wipe your game progression?\nThis will remove all unlocks, scores and level progression." delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[resetGameAlert show];
}
else if (theTag.intValue == 2) { // credits menu
// play sound
[self menuButtonPressed];
theScene = [[CreditsScene alloc] initWithSize:self.view.bounds.size];
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
[self.view presentScene:theScene transition:theTransition];
}
else if (theTag.intValue == 3) { // main menu
// play sound
[self menuButtonPressed];
// scene to move to
theScene = [[MainMenuScene alloc] initWithSize:self.view.bounds.size];
// transition type
theTransition = [SKTransition flipHorizontalWithDuration:1.0];
[self.view presentScene:theScene transition:theTransition];
}
}
#pragma mark - RESET TUTORIALS
-(void)resetTutorials{
for (id theObject in [[UserDetails sharedManager]userTutorials]) {
TutorialFlowerTarget *theTut= [[[UserDetails sharedManager]userTutorials]objectForKey:theObject];
[theTut setTriggered:NO];
}
[[UserDetails sharedManager]saveData];
}
#pragma mark - RESET UNLOCKS
-(void)resetUnlocks{
[[UnlockController sharedManager]resetGame];
}
I could be wrong but I suspect your button class is your offender. When you call...
[testButton4 addTarget:self selector:#selector(buttonPressed:) withObject:[NSNumber numberWithInt:5] forControlEvent:AGButtonControlEventTouchUpInside];
You pass self being that scene. In the button class the method..
-(void)addTarget:(id)target selector:(SEL)selector withObject:(id)object forControlEvent:(AGButtonControlEvent)controlEvent
{
//check whether selector is already saved, otherwise it will get called twice
if (marrSelectors == nil)
{
marrSelectors = [NSMutableArray new];
}
NSMutableDictionary *mdicSelector = [[NSMutableDictionary alloc]init];
[mdicSelector setObject:target forKey:#"target"];
[mdicSelector setObject:[NSValue valueWithPointer:selector] forKey:#"selector"];
if (object)
{
[mdicSelector setObject:object forKey:#"object"];
}
[mdicSelector setObject:[NSNumber numberWithInt:controlEvent] forKey:#"controlEvent"];
[marrSelectors addObject:mdicSelector];
}
Note this..
[mdicSelector setObject:target forKey:#"target"];
target is your scene and it is being tossed into a dictionary in that is tossed into an array. So in theory that button now has a strong reference to your scene and your scene has a reference to that button.
To test this theory set all of your buttons to nil before calling
[self.view presentScene:theScene transition:theTransition];
And if I am correct that should break the strong reference to each other. Hopefully that helps and is the issue.
In a game I'm working on I have a method for going on to the next scene. It looks like this:
func proceedToNextLevel() {
// Go to next level
self.physicsWorld.removeAllJoints()
self.enumerateChildNodesWithName("cube", usingBlock: {
(node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
// do something with node or stop
node.removeFromParent()
})
player.removeFromParent()
hud.removeFromParent()
}
Each level is a scene that inherits this method. (I have a GameScene.swift, and every level is a subclass of it)
This method in each level looks like this:
override func proceedToNextLevel() {
super.proceedToNextLevel()
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Right, duration: 3)
let newScene: LevelFourScene = LevelFourScene.unarchiveFromFile("LevelFourScene") as! LevelFourScene
newScene.scaleMode = SKSceneScaleMode.AspectFit
self.view?.presentScene(newScene)
}
Obviously this is swift and your game is in Objective C but hopefully you get the idea.
Based on your posted code, I suggest you add this to your MainMenu:
-(void) willMoveFromView:(SKView *)view {
[self removeAllChildren];
backgroundImage = nil;
topBar = nil;
bottomBar = nil;
ladybirds = nil;
title = nil;
subtitle = nil;
coffee = nil;
settingsWin = nil;
iapWin = nil;
unlocksWin = nil;
startButton = nil;
continueButton = nil;
settingsButton = nil;
iapButton = nil;
unlocksButton = nil;
theParticles = nil;
}
Add this to your SettingsScene:
-(void) willMoveFromView:(SKView *)view {
[self removeAllChildren];
backButton = nil;
resetButton = nil;
resetTutorialsButton = nil;
creditsButton = nil;
titleLabel = nil;
copyrightLabel = nil;
resetGameAlert = nil;
resetTutAlert = nil;
theScene = nil;
theTransition = nil;
menuBg = nil;
}
Keep in mind that SK uses its own caching which you have no control over. This means you will see an increase in memory the first couple of times you switch scenes but it will level out at some point. I suggest you move back and forth 15 to 20 times and see what happens.
Related
I am trying to implement a custom NSTextField that a) changes color when active and b) has a label in the top left hand corner.
I have the following implementation:
In TestTextField.h
#interface TestTextField : NSTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset;
...
#end
In TestTextField.m
#interface TestTextField() {
BOOL _thisFieldIsActive;
}
#property (nonatomic, strong) NSTextField *label;
#end
#implementation TestTextField
- (id)initFullWidthWithLabel:(NSString *)label andPreset:(NSString *)preset {
self = [super initWithFrame:NSZeroRect];
if (self) {
_thisFieldIsActive = NO;
[self setFocusRingType:NSFocusRingTypeNone];
// small label top left
_label = [[NSTextField alloc] initWithFrame:NSZeroRect];
_label.stringValue = label;
if (preset) {
self.stringValue = preset;
}
else {
self.stringValue = #"0";
}
[self layoutUI];
}
return self;
}
- (void)turnActiveOff {
[self toggleActive:NO];
}
- (void)toggleActive:(BOOL)active {
_thisFieldIsActive = active;
if (_thisFieldIsActive) {
self.backgroundColor = [NSColor blueColor];
self.textColor = [NSColor whiteColor];
_label.textColor = [NSColor whiteColor];
}
else {
self.backgroundColor = [NSColor clearColor];
self.textColor = [NSColor blackColor];
_label.textColor = [NSColor grayColor];
}
}
- (BOOL)becomeFirstResponder {
NSLog(#"BecomeFirstResponder");
[self selectText:self];
[self toggleActive:YES];
return [super becomeFirstResponder];
}
- (void)textDidEndEditing:(NSNotification *)notification {
NSLog(#"DidEndEditing");
[self toggleActive:NO];
[super textDidEndEditing:notification];
}
- (void)layoutUI {
self.alignment = NSRightTextAlignment;
self.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:32.0f];
self.layer.borderColor = [NSColor whiteColor].CGColor;
self.layer.borderWidth = 1.0f;
[self.layer setCornerRadius:4.0f];
// small label top left
_label.font = [NSFont fontWithName:#"HelveticaNeue-Light" size:12.0f];
_label.alignment = NSLeftTextAlignment;
_label.textColor = [NSColor grayColor];
_label.stringValue = [_label.stringValue uppercaseString];
_label.selectable = NO;
_label.editable = NO;
_label.drawsBackground = NO;
_label.bezeled = NO;
[self addSubview:_label];
NSDictionary *metrics = #{#"borderPadding": #5};
_label.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(borderPadding)-[_label(100)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(borderPadding)-[_label(30)]" options:0 metrics:metrics views:#{ #"_label" : _label }]];
}
In my ViewController I implement these TestTextFields by simply calling the custom initFullWidthWithLabel:andPreset: method and adding them as a subview to the VCs view
I can see that the label gets positioned correctly, however as soon as the the field becomes active and the backgroundColor is set, it seems to cover up the label. How can I make sure the label stays on top?
Even when the backgorund coloring is turned off, the label remains hidden.
Thanks
The core of the solution was to use a custom subclassed NSTextFieldCell with one method:
- (NSRect)drawingRectForBounds:(NSRect)rect {
NSRect rectInset = NSMakeRect(rect.origin.x + 100.0f, rect.origin.y, rect.size.width - 100.0f, rect.size.height);
return [super drawingRectForBounds:rectInset];
}
Showing a UIPickerView with UIActionSheet in iOS8 not showing
-(void)showPicker{
/************************ FIXED please contact me on nfsarmento#hotmail.com if you need help to fix******//
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
switch((uint)currentDelegate){
case 0:{
pickerView.dataSource = propertyDelegate;
pickerView.delegate = propertyDelegate;
[pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO];
break;
}
case 1:{
pickerView.dataSource = regionDelegate;
pickerView.delegate = regionDelegate;
[pickerView selectRow:[regionDelegate index] inComponent:0 animated:NO];
break;
}
case 2:{
pickerView.dataSource = townDelegate;
pickerView.delegate = townDelegate;
[pickerView selectRow:[townDelegate index] inComponent:0 animated:NO];
break;
}
case 3:{
pickerView.dataSource = bedDelegate;
pickerView.delegate = bedDelegate;
[pickerView selectRow:[bedDelegate index] inComponent:0 animated:NO];
break;
}
case 4:{
pickerView.dataSource = bathDelegate;
pickerView.delegate = bathDelegate;
[pickerView selectRow:[bathDelegate index] inComponent:0 animated:NO];
break;
}
case 5:{
pickerView.dataSource = priceDelegate;
pickerView.delegate = priceDelegate;
[pickerView selectRow:[priceDelegate index1] inComponent:0 animated:NO];
[pickerView selectRow:[priceDelegate index2] inComponent:1 animated:NO];
break;
}
case 6:{
pickerView.dataSource = currencyDelegate;
pickerView.delegate = currencyDelegate;
[pickerView selectRow:[currencyDelegate index] inComponent:0 animated:NO];
break;
}
}
[actionSheet addSubview:pickerView];
UISegmentedControl *previousButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Previous", nil)]];
previousButton.momentary = YES;
previousButton.frame = CGRectMake(5.0f, 7.0f, 70.0f, 30.0f);
previousButton.segmentedControlStyle = UISegmentedControlStyleBar;
previousButton.tintColor = [UIColor blackColor];
UISegmentedControl *nextButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Next", nil)]];
nextButton.momentary = YES;
nextButton.frame = CGRectMake(80.0f, 7.0f, 70.0f, 30.0f);
nextButton.segmentedControlStyle = UISegmentedControlStyleBar;
nextButton.tintColor = [UIColor blackColor];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"Done", nil)]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor colorWithRed:0.35f green:0.55f blue:1.5f alpha:1.0f];
[closeButton addTarget:self action:#selector(hidePicker) forControlEvents:UIControlEventValueChanged];
[previousButton addTarget:self action:#selector(previousPicker) forControlEvents:UIControlEventValueChanged];
[nextButton addTarget:self action:#selector(nextPicker) forControlEvents:UIControlEventValueChanged];
if(currentDelegate > 0)[actionSheet addSubview:previousButton];
if(currentDelegate < 6)[actionSheet addSubview:nextButton];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void) nextPicker{
[self hidePicker];
currentDelegate++;
[self showPicker];
}
-(void) previousPicker{
[self hidePicker];
currentDelegate--;
[self showPicker];
}
-(void)hidePicker{
switch ((uint)currentDelegate) {
case 0:{
[tf_type setText: [propertyDelegate.values objectAtIndex: [propertyDelegate index]]];
_appDelegate.int_typeV = [propertyDelegate index];
break;
}
case 1:{
[tf_region setText: [regionDelegate.values objectAtIndex: [regionDelegate index]]];
_appDelegate.int_regionV = [regionDelegate index];
//Reset Town dropdown when a region is picked
_appDelegate.int_townV = 0;
townDelegate = [[TownDelegate alloc] init];
[self.tf_town setText: [townDelegate.values objectAtIndex: [_appDelegate int_townV]]];
break;
}
case 2:{
[tf_town setText: [townDelegate.values objectAtIndex: [townDelegate index]]];
_appDelegate.int_townV = [townDelegate index];
break;
}
case 3:{
[tf_numBed setText: [bedDelegate.values objectAtIndex: [bedDelegate index]]];
_appDelegate.int_numBedV = [bedDelegate index];
break;
}
case 4:{
[tf_numBath setText: [bathDelegate.values objectAtIndex: [bathDelegate index]]];
_appDelegate.int_numBathV = [bathDelegate index];
break;
}
case 5:{
NSString * priceString = [[NSString alloc] initWithFormat:#"%# - %#",
[priceDelegate.values objectAtIndex: [priceDelegate index1]],
[priceDelegate.values2 objectAtIndex: [priceDelegate index2]]];
[tf_price setText:priceString];
_appDelegate.int_minPriceV = [priceDelegate index1];
_appDelegate.int_maxPriceV = [priceDelegate index2];
break;
}
case 6:{
[tf_currency setText: [currencyDelegate.values objectAtIndex: [currencyDelegate index]]];
_appDelegate.int_currencyV = [currencyDelegate index];
break;
}
}
[popoverController dismissPopoverAnimated:YES];
popoverController = nil;
[self dismissActionSheet];
}
My picker view
#import "PropertyTypeDelegate.h"
#implementation PropertyTypeDelegate
#synthesize values;
-(id)init{
self = [super init];
[self loadData];
return self;
}
-(int)index{ return index;}
-(void) loadData{
NSArray* array = [[NSArray alloc] initWithObjects:
NSLocalizedString(#"No Preference", nil),
NSLocalizedString(#"Villa", nil),
NSLocalizedString(#"Town House", nil),
NSLocalizedString(#"Apartment", nil),
NSLocalizedString(#"Retail", nil),
NSLocalizedString(#"Labour Camp", nil),
NSLocalizedString(#"Office", nil),
NSLocalizedString(#"Warehouse", nil),
NSLocalizedString(#"Land Residential", nil),
NSLocalizedString(#"Hotel apartment", nil),
NSLocalizedString(#"Residential Building", nil),
nil];
self.values = array;
index = 0;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [values count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{ return [values objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *) thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
index = (int) (NSInteger)row;
}
#end
The link provided above actually refers to Apple Doc where it has removed adding subview to UIActionSheet. In your code, you are doing similar thing by adding UIPickerView into UIActionSheet. So in iOS8 onwards, even if the view is added to UIActionSheet, the view returned is actually nil while displaying.
For this purpose you can use ActionSheetPicker-3.0.
Actually, it's not UIActionSheet anymore. But looks exactly the same, and that's why it works on iOS8.
Do let me know if this answers your query!
I have an iPad app that I want to add a barcode reader to... this is the code for the initialization of the barcoder code:
-(void) scanInitializationCode {
_highlightView = [[UIView alloc] init];
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor = [UIColor greenColor].CGColor;
_highlightView.layer.borderWidth = 3;
[self.view addSubview:_highlightView];
// define the label to display the results of the scan
_label = [[UILabel alloc] init];
_label.frame = CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40);
_label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
_label.backgroundColor = [UIColor colorWithWhite:0.15 alpha:0.65];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.text = #"(none)";
[self.view addSubview:_label];
// session initialization
_session = [[AVCaptureSession alloc] init];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
// define the input device
_input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
if (_input) {
[_session addInput:_input];
} else {
NSLog(#"Error: %#", error);
}
// and output device
_output = [[AVCaptureMetadataOutput alloc] init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:_output];
_output.metadataObjectTypes = [_output availableMetadataObjectTypes];
// and preview layer
_prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
_prevLayer.frame = self.view.bounds;
_prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:_prevLayer];
}
This is the AVCaptureMetadataOutputObjectsDelegate code:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = #[AVMetadataObjectTypeEAN13Code];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil) {
_label.text = detectionString;
oISBNField.text = detectionString; // move detectionString to ISBN textbox
[_session stopRunning];
[_highlightView removeFromSuperview];
break;
}
else
_label.text = #"(none)";
}
This is the code that starts the scanning process by having the user tap a UIButton:
- (IBAction)aReadBarcode:(UIButton *)sender {
[self scanInitializationCode];
[_session startRunning];
// display the activity
[self.view bringSubviewToFront:_highlightView];
[self.view bringSubviewToFront:_label];
oISBNField.text = scanResults;
}
The problem is that once the scan has found the barcode, it stays visible; what I want to do is have it return to the UIView that has the button that caused it to start scanning (in other words, I want the _highlightView to disappear). I have tried all kinds of "dismissal" methods, even putting it at the back of the z-order, but none of them work. How can I make the highlightView disappear from the screen?
The answer:
[_prevLayer removeFromSuperlayer]; after [_session stopRunning]
I am using the zbarsdk to read the barcodes . It is working fine , but my problem is I have added the overlay view to the ZBarReaderViewController (reader in my code ). So now I tried to add the tap to focus functionality . But it is crashing . Below is my code . Thanks in advance . Any ideas would be grateful .
-(IBAction)scanBarCode:(id)sender
{
barcodeClicked = 1;
NSLog(#"TBD: scan barcode here...");
// ADD: present a barcode reader that scans from the camera feed
reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
reader.showsZBarControls = NO;
UIView *ovlView = [[UIView alloc] init];
[ovlView setFrame:CGRectMake(0, 0, 320, 480)];
[ovlView setBackgroundColor:[UIColor clearColor]];
UIImageView *leftBracket = [[UIImageView alloc] init];
[leftBracket setFrame:CGRectMake(21, 100, 278, 15)];
[leftBracket setImage:[UIImage imageNamed:#"TopBracket.png"]];
UIImageView *rightBracket = [[UIImageView alloc] init];
[rightBracket setFrame:CGRectMake(21, 240, 278, 15)];
[rightBracket setImage:[UIImage imageNamed:#"BottomBracket.png"]];
UIToolbar *bottomBar = [[UIToolbar alloc] init];
[bottomBar setBarStyle:UIBarStyleBlackOpaque];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale >= 1136)
{
[bottomBar setFrame:CGRectMake(0, 524, 320, 44)];
}
else
[bottomBar setFrame:CGRectMake(0, 436, 320, 44)];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelCamera)];
/*UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];*/
//UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:#" Info " style:UIBarButtonItemStyleBordered target:self action:#selector(infoButton)];
/*UIButton *info = [UIButton buttonWithType:UIButtonTypeInfoLight];
[info addTarget:self action:#selector(infoButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:info];
[bottomBar setItems:[NSArray arrayWithObjects:cancel,flexItem,infoButton, nil]];*/
[bottomBar setItems:[NSArray arrayWithObjects:cancel, nil]];
[ovlView addSubview:leftBracket];
[ovlView addSubview:rightBracket];
[ovlView addSubview:bottomBar];
reader.cameraOverlayView = ovlView;
// present and release the controller
[self presentModalViewController:reader
animated: YES];
[reader release];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIView * previewView = [[[[[[[[[[
reader.view // UILayoutContainerView
subviews] objectAtIndex:0] // UINavigationTransitionView
subviews] objectAtIndex:0] // UIViewControllerWrapperView
subviews] objectAtIndex:0] // UIView
subviews] objectAtIndex:0] // PLCameraView
subviews] objectAtIndex:0]; // PLPreviewView
[previewView touchesBegan:touches withEvent:event];
}
Thanks for MacN00b's answer! That points out the right direction to go for me. I have implemented tap-focus for zbarViewController. Here is the idea:
You can add a custom view to zbarViewController by assigning a custom view to its cameraOverlayView. Then add a TapGestureRecagonizer to the custom view to catch the tap. Then, get the touch point and make the camera focus to the touch point. You would like possibly add a little rectangle around the touch point(that is what I did).
Here goes the code(assigning the custom view to cameraOverlayView:
UIView *view = [[UIView alloc] init];
UITapGestureRecognizer* tapScanner = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(focusAtPoint:)];
[view addGestureRecognizer:tapScanner];
reader.cameraOverlayView = view;
Then in selector focusAtPoint:
- (void)focusAtPoint:(id) sender{
CGPoint touchPoint = [(UITapGestureRecognizer*)sender locationInView:_reader.cameraOverlayView];
double focus_x = touchPoint.x/_reader.cameraOverlayView.frame.size.width;
double focus_y = (touchPoint.y+66)/_reader.cameraOverlayView.frame.size.height;
NSError *error;
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *device in devices){
NSLog(#"Device name: %#", [device localizedName]);
if ([device hasMediaType:AVMediaTypeVideo]) {
if ([device position] == AVCaptureDevicePositionBack) {
NSLog(#"Device position : back");
CGPoint point = CGPointMake(focus_y, 1-focus_x);
if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){
[device setFocusPointOfInterest:point];
CGRect rect = CGRectMake(touchPoint.x-30, touchPoint.y-30, 60, 60);
UIView *focusRect = [[UIView alloc] initWithFrame:rect];
focusRect.layer.borderColor = [UIColor whiteColor].CGColor;
focusRect.layer.borderWidth = 2;
focusRect.tag = 99;
[_reader.cameraOverlayView addSubview:focusRect];
[NSTimer scheduledTimerWithTimeInterval: 1
target: self
selector: #selector(dismissFocusRect)
userInfo: nil
repeats: NO];
[device setFocusMode:AVCaptureFocusModeAutoFocus];
[device unlockForConfiguration];
}
}
}
}
}
I have added a white rectangle around the touch point, and then use selector dismissFocusRect to dismiss this rectangle. Here is the code:
- (void) dismissFocusRect{
for (UIView *subView in _reader.cameraOverlayView.subviews)
{
if (subView.tag == 99)
{
[subView removeFromSuperview];
}
}
}
I hope this could help!
Look at this documentation by apple in the "Focus Modes" section: https://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html It talks all about how to implement tap to focus properly. I would try to implement this by
CGRect screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
double focus_x = thisFocusPoint.center.x/screenWidth;
double focus_y = thisFocusPoint.center.y/screenHeight;
[[self captureManager].videoDevice lockForConfiguration:&error];
[[self captureManager].videoDevice setFocusPointOfInterest:CGPointMake(focus_x,focus_y)];
Well if you are using that view controller, how about adding a (void) that should be ok to implement in the barcodeviewcontroller.
- (void) focusAtPoint:(CGPoint)point
{
AVCaptureDevice *device = [[self videoInput] device];
if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
NSError *error;
if ([device lockForConfiguration:&error]) {
[device setFocusPointOfInterest:point];
[device setFocusMode:AVCaptureFocusModeAutoFocus];
[device unlockForConfiguration];
} else {
id delegate = [self delegate];
if ([delegate respondsToSelector:#selector(acquiringDeviceLockFailedWithError:)]) {
[delegate acquiringDeviceLockFailedWithError:error];
}
}
}
}
I am Using AVFoundation's AVCaptureSession to capture the video and I am using the MPMoviePlayerController to play the streamed url(video) from server. When I am capturing only video with AVCaptureSession there is no problem. But When I tried to play the streamed url(with MPMoviePlayerController) along with capturing of video with AVCaptureSession at a time the problem occurs as the capturing from AVCaptureSession stops.
This is what I had done:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
if([appDelegate isIpad] == YES)
controlsView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+200, self.view.bounds.origin.y+250, self.view.bounds.size.width, self.view.bounds.size.height)];
else
controlsView = [[UIView alloc] initWithFrame:self.view.bounds];
controlsView.backgroundColor = [UIColor blackColor];
[self.view addSubview:controlsView];
[self.view sendSubviewToBack:controlsView];
//settingsBtn = [[UIButton alloc]initWithFrame:CGRectMake(10,400, 50, 50)];
settingsBtn = [[UIButton alloc]initWithFrame:CGRectMake(10,400, 50, 50)];
[settingsBtn setImage:[UIImage imageNamed:#"settings.png"] forState:UIControlStateNormal];
[settingsBtn addTarget:self action:#selector(settingsAction) forControlEvents:UIControlEventTouchUpInside];
[controlsView addSubview:settingsBtn];
callButton = [[UIButton alloc]initWithFrame:CGRectMake(260,400, 50, 50)];
if(isCallButtonClicked ==NO)
[callButton setImage:[UIImage imageNamed:#"call.png"] forState:UIControlStateNormal];
else
[callButton setImage:[UIImage imageNamed:#"callEnd.png"] forState:UIControlStateNormal];
[callButton addTarget:self action:#selector(callAction) forControlEvents:UIControlEventTouchUpInside];
[controlsView addSubview:callButton];
statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 20, 150, 40)];
statusLabel.textColor = [UIColor whiteColor];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.font = [UIFont boldSystemFontOfSize:20];
dot1 = [[UIView alloc] initWithFrame:CGRectMake(75, 20, 7, 7)];
dot1.layer.cornerRadius = 5;
dot1.backgroundColor = [UIColor whiteColor];
[statusLabel addSubview:dot1];
dot2 = [[UIView alloc] initWithFrame:CGRectMake(84, 20, 7, 7)];
dot2.layer.cornerRadius = 5;
dot2.backgroundColor = [UIColor whiteColor];
[statusLabel addSubview:dot2];
dot3 = [[UIView alloc] initWithFrame:CGRectMake(93, 20, 7, 7)];
dot3.layer.cornerRadius = 5;
dot3.backgroundColor = [UIColor whiteColor];
[statusLabel addSubview:dot3];
downStreamView = [[UIView alloc]initWithFrame:CGRectMake(controlsView.bounds.origin.x, controlsView.bounds.origin.y, controlsView.bounds.size.width, controlsView.bounds.size.height - 70)];
[[controlsView layer] addSublayer:downStreamView.layer];
downStreamView.layer.backgroundColor = [UIColor greenColor].CGColor;
AVCaptureSession *captureSession = [[AVCaptureSession alloc]init];
NSError *error;
/* getting the device input */
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:[self frontFacingCamera] error:&error];
if(error)
{
NSLog(#"%#",#"Could not create video input");
}
[captureSession addInput:videoInput];
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:[self audioDevice] error:&error];
[captureSession addInput:audioInput];
audioOutput = [[AVCaptureAudioDataOutput alloc]init];
[captureSession addOutput:audioOutput];
previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSession];
[previewLayer setFrame:CGRectMake(controlsView.bounds.origin.x, controlsView.bounds.origin.y, controlsView.bounds.size.width, controlsView.bounds.size.height - 70)];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[[controlsView layer] addSublayer:previewLayer];
[captureSession startRunning];
}
//make call Action
-(void)makeCallAction
{
if(isCallButtonClicked == NO)
{
statusLabel.text = #"Dialling";
[controlsView addSubview:statusLabel];
if(!isAnimationStarted)
[self animate];
[callButton setImage:[UIImage imageNamed:#"callEnd.png"] forState:UIControlStateNormal];
[UIView animateWithDuration:2.0
animations:^{
CGRect frame = CGRectMake(downStreamView.layer.bounds.origin.x, downStreamView.layer.bounds.size.height-100, 100, 100);
previewLayer.frame = frame;
[downStreamView.layer addSublayer:previewLayer];
}
completion:^(BOOL finished){
//Do nothing
}];
isCallButtonClicked = YES;
}
else if(isCallButtonClicked == YES)
{
[statusLabel removeFromSuperview];
[callButton setImage:[UIImage imageNamed:#"call.png"] forState:UIControlStateNormal];
[UIView animateWithDuration:2.0
animations:^{
[previewLayer setFrame:CGRectMake(controlsView.bounds.origin.x, controlsView.bounds.origin.y, controlsView.bounds.size.width, controlsView.bounds.size.height - 70)];
[[controlsView layer] addSublayer:previewLayer];
}
completion:^(BOOL finished){
//Do nothing
}];
isCallButtonClicked = NO;
}
}
//Settings Action
-(void)settingsAction
{
NSString *nibName = nil;
if ([[[UIDevice currentDevice] model] isEqualToString:#"iPhone"] || [[[UIDevice currentDevice] model] isEqualToString:#"iPhone Simulator"]) {
nibName = #"SettingsViewController";
}
else {
nibName = #"SettingsViewController_iPad";
}
SettingsViewController *settingsController = [[SettingsViewController alloc]initWithNibName:nibName bundle:nil];
[self.navigationController presentModalViewController:settingsController animated:YES];
}
-(void)callAction
{
NSURL *theMovieURL = [NSURL URLWithString:#"someURL.m3u8"];
if (theMovieURL)
{
if ([theMovieURL scheme]) // sanity check on the URL
{
/* Play the movie with the specified URL. */
[self playStreamingURL:theMovieURL];
}
}
[self makeCallAction];
}
-(void)playStreamingURL:(NSURL *)aUrlStr
{
MPMovieSourceType movieSourceType = MPMovieSourceTypeUnknown;
/* If we have a streaming url then specify the movie source type. */
if ([[aUrlStr pathExtension] compare:#"m3u8" options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
movieSourceType = MPMovieSourceTypeStreaming;
}
[self createAndPlayMovieForURL:aUrlStr sourceType:movieSourceType];
}
-(void)createAndPlayMovieForURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType
{
[self createAndConfigurePlayerWithURL:movieURL sourceType:sourceType];
/* making the player to be visible in full screen mode */
//if(!self.moviePlayerController.fullscreen)
// self.moviePlayerController.fullscreen = YES;
/* disabling the controls of the movie player */
self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
/* Play the movie! */
[[self moviePlayerController] play];
}
-(void)createAndConfigurePlayerWithURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType
{
[controlsView addSubview:downStreamView];
/* Create a new movie player object. */
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (player)
{
/* Save the movie object. */
[self setMoviePlayerController:player];
player.contentURL = MPMovieControlStyleNone;
//if(!player.fullscreen)
// player.fullscreen = YES;
/* Register the current object as an observer for the movie
notifications. */
// [self installMovieNotificationObservers];
/* Specify the URL that points to the movie file. */
[player setContentURL:movieURL];
/* If you specify the movie type before playing the movie it can result
in faster load times. */
[player setMovieSourceType:sourceType];
/* Apply the user movie preference settings to the movie player object. */
//[self applyUserSettingsToMoviePlayer];
/* Add a background view as a subview to hide our other view controls
underneath during movie playback. */
//CGRect viewInsetRect = CGRectInset ([self.view bounds],
// kMovieViewOffsetX,
//kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[player view] setFrame:downStreamView.bounds];
[player view].backgroundColor = [UIColor redColor];
/* To present a movie in your application, incorporate the view contained
in a movie player’s view property into your application’s view hierarchy.
Be sure to size the frame correctly. */
[downStreamView.layer addSublayer: [player view].layer];
}
}
-(void)installMovieNotificationObservers
{
MPMoviePlayerController *player = [self moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(mediaIsPreparedToPlayDidChange:)
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:player];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:player];
}
/* Notification called when the movie finished playing. */
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
NSNumber *reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason integerValue])
{
/* The end of the movie was reached. */
case MPMovieFinishReasonPlaybackEnded:
/*
Add your code here to handle MPMovieFinishReasonPlaybackEnded.
*/
break;
/* An error was encountered during playback. */
case MPMovieFinishReasonPlaybackError:
NSLog(#"An error was encountered during playback");
[self performSelectorOnMainThread:#selector(displayError:) withObject:[[notification userInfo] objectForKey:#"error"] waitUntilDone:NO];
[self removeMovieViewFromViewHierarchy];
break;
/* The user stopped playback. */
case MPMovieFinishReasonUserExited:
[self removeMovieViewFromViewHierarchy];
break;
default:
break;
}
}
/* Remove the movie view from the view hierarchy. */
-(void)removeMovieViewFromViewHierarchy
{
MPMoviePlayerController *player = [self moviePlayerController];
[player.view removeFromSuperview];
}
- (void)animate {
isAnimationStarted = YES;
//First Animation
[UIView animateWithDuration:0.5 animations:^{
dot1.alpha = 1;
dot2.alpha = 0.5;
dot3.alpha = 0.5;
} completion:^(BOOL finished) {
//2nd Animation
[UIView animateWithDuration:0.5 animations:^{
dot1.alpha = 0.5;
dot2.alpha = 1;
dot3.alpha = 0.5;
} completion:^(BOOL finished) {
//3rd Animation
[UIView animateWithDuration:0.5 animations:^{
dot1.alpha = 0.5;
dot2.alpha = 0.5;
dot3.alpha = 1;
} completion:^(BOOL finished) {
[self performSelector:#selector(animate)];
}];
}];
}];
}
// Find a camera with the specificed AVCaptureDevicePosition, returning nil if one is not found
- (AVCaptureDevice *) cameraWithPosition:(AVCaptureDevicePosition) position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
return nil;
}
// Find a front facing camera, returning nil if one is not found
- (AVCaptureDevice *) frontFacingCamera
{
return [self cameraWithPosition:AVCaptureDevicePositionFront];
}
// Find a back facing camera, returning nil if one is not found
- (AVCaptureDevice *) backFacingCamera
{
return [self cameraWithPosition:AVCaptureDevicePositionBack];
}
- (AVCaptureDevice *) audioDevice
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
if ([devices count] > 0) {
return [devices objectAtIndex:0];
}
return nil;
}
-(void)audioData:(id)info
{
NSArray *connections = audioOutput.connections;
AVCaptureConnection *connection = [connections objectAtIndex:0];
NSArray *audioChannels = connection.audioChannels;
AVCaptureAudioChannel *audioChannel = [audioChannels objectAtIndex:0];
//[label setText:[NSString stringWithFormat:#"%f", audioChannel.averagePowerLevel]];
}
Guy's Please help me how to resolve this issue :(
Regards