Hiding a button sprite in xcode - iphone

Please help me, I've been trying for hours to get this to work. I'm kind of new to this type of programming so bear with me. I'm trying to create a popover help image that has a close button in the top corner. When you click the close button the popover closes. sounds simple enough right? I've currently got it so that when the button is clicked the popover image closes, but i can't get the actual close button to go away at the same time. what am i missing here?
-(void) startGame
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
showHelp = [CCSprite spriteWithFile: #"help.png"];
showHelp.position = ccp(winSize.width / 2, winSize.height / 2);
[self addChild: showHelp];
if (g_isFirst)
{
showHelp.visible = YES;
}
else
{
showHelp.visible = YES;
}
GrowButton* button = [GrowButton buttonWithSprite:#"close_def.png"
selectImage:#"close_def.png"
target:self
selector:#selector(selCloseHelp)];
float x = ADJUST_X(410)+(IS_IPAD()?60:0) + (IS_IPHONE5?44:0);
button.position = ccp(x, ADJUST_Y(270)+(IS_IPAD()?60:0));
[self addChild: button];
}
-(void) selCloseHelp
{
if (g_SoundFlag)
{
[sd_button play];
showHelp.visible = NO;
}
}
my problem is that i have no idea what to add to make the close button hidden as well. thanks in advance.

You have two choices:
Add the button as a subview of showHelp.
make button a property ( like showHelp) and set its visible to NO also.

yourSprite.visible=NO;
if you used the visible property to set the sprite hidden it is very good approch to hide the objcet because if you set the Visible=no then sprite may not be created in memory thats why the memory will not be wastage.
simply do this stuff

make the button a property and set
yourButton.hidden = YES;

Related

Adding StatusBar at the bottom of iPhone

I want to show a status bar in the bottom of the iPhone like the one in Gmail account that is appear to indicate that it is checking mail. I have tried the following solution in this thread
Adding view on StatusBar in iPhone
but the status bar didn't appear, then i used the same code without any modification to show it in the top of the default status bar and it also didn't appear.
i have tried also another solution using MTStatusBarOverlay, when i tried to change its frame to be at the bottom i got a black rectangl in the middle of the screen
any help?
here is the code
// new class i have created
#interface BottomStatusBarOverlay : UIWindow
#end
#implementation BottomStatusBarOverlay
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Place the window on the correct level and position
self.windowLevel = UIWindowLevelStatusBar+1.0f;
self.frame = [[UIApplication sharedApplication] statusBarFrame];
self.alpha = 1;
self.hidden = NO;
// Create an image view with an image to make it look like a status bar.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [[UIImage imageNamed:#"statusBarBackgroundGrey.png"] stretchableImageWithLeftCapWidth:2.0f topCapHeight:0.0f];
[self addSubview:backgroundImageView];
}
return self;
}
#end
// usage in my view controller in a button action
#implementation MainViewController
-(IBAction)showBottomStatusbar:(id)sender {
BottomStatusBarOverlay *bottomStatusBarOverlay = [[BottomStatusBarOverlay alloc] init];
bottomStatusBarOverlay.hidden = NO;
}
The code you posted shows that your showBottomStatusbar method creates a BottomStatusBarOverlay instance, but you never actually add it as a subview to anything.
I don't use the Gmail app on iPhone. So, I'm not sure what it looks like or how it functions. However, I have create a notification bar in the past that seems similar to what you described. It animates on to the bottom of the screen, shows a message for three seconds, and then slides back off. I accomplished this by adding the bar to the application's window, which will ensure it overlays any view the application is currently showing. You could, however, add the bar to any view that is currently active, if you don't need a global bar within your app. Here's how you get the app's window reference:
UIApplication* app = [UIApplication sharedApplication];
UIWindow* appWin = app.delegate.window;
To animate, you can use animateWithDuration, like so:
[UIView animateWithDuration:0.3
animations:^ {
// However you want to animate on to the screen.
// This will slide it up from the bottom, assuming the
// view's start position was below the screen.
view.frame = CGRectMake(0,
winHeight - viewHeight,
winWidth,
viewHeight);
}
completion:^(BOOL finished) {
// Schedule a timer to call a dismiss method after
// a set period of time, which would probably perform
// an animation off the screen.
dismissTimer = [NSTimer
scheduledTimerWithTimeInterval:3
target:globalMessage
selector:#selector(dismiss)
userInfo:nil
repeats:NO];
}];
Hope this helps.

Is there anyway to have a the menu work even when I pause the game?

So I am making a menu:
CCMenu *menu = [CCMenu menuWithItems:addThing1, addThing2, addThing3, nil];
[menu alignItemsVertically];
[self addChild:menu];
It works fine, but I also want to pause the game when this happens, so I do this:
[[CCDirector sharedDirector] pause];
So, this works too, but the problem is that when I pause the game, it pauses the menu too. Is there anyway for this not to happen? It seems to me that its counter intuitive to pause the menu too... But anyways: Is there a way to make it not pause the menu? Or do I have to resort to other methods... I will if I have to but want to know if there is any relatively quick fix to this code that will make it work...
Well... I never use [[CCDirector sharedDirector] pause]. It pauses all of Cocos2D and that isn't what you want here. What I am not clear on is if you want a menu that takes over the screen, or if you want a menu on top of the game but you can still see the game.
Add full screen menu:
Create a new CCLayer subclass and name it whatever you want. Set it up how you like it and then use:
// Push the PauseLayer screen (or whatever you have called your menu
// layer class)
// This will pause your game and hold it's state in memory.
[[CCDirector sharedDirector] pushScene:[PauseLayer node]];
// Pop the cached scene
// This will remove your menu layer and display your
// game layer where it was left off.
// Any custom timers will likely need to be
// reset/resumed inside of onEnter or onEnterTransitionDidFinish
[[CCDirector sharedDirector] popScene];
Add a overlay type menu:
To get a menu where you can still see the game underneath you will want to do something like this.
CCLabelBMFont *difficultyLabel = [CCLabelBMFont labelWithString:#"Difficulty" fntFile:#"projectOneMenuItem1.fnt"];
CCMenuItemLabel *difficulty = [CCMenuItemLabel itemWithLabel:difficultyLabel target:self selector:#selector(chooseDifficulty:)];
CCLabelBMFont *audioSourceLabel = [CCLabelBMFont labelWithString:#"Switch to iPod Audio" fntFile:#"projectOneMenuItem1.fnt"];
CCMenuItemLabel *audioSource = [CCMenuItemLabel itemWithLabel:audioSourceLabel target:self selector:#selector(switchAudioSource:)];
CCLabelBMFont *leaderboardsLabel = [CCLabelBMFont labelWithString:#"Leaderboards" fntFile:#"projectOneMenuItem1.fnt"];
CCMenuItemLabel *leaderboards = [CCMenuItemLabel itemWithLabel:leaderboardsLabel target:self selector:#selector(showLeaderboards:)];
CCLabelBMFont *achievementsLabel = [CCLabelBMFont labelWithString:#"Achievements" fntFile:#"projectOneMenuItem1.fnt"];
CCMenuItemLabel *achievements = [CCMenuItemLabel itemWithLabel:achievementsLabel target:self selector:#selector(showAchievements:)];
CCLabelBMFont *backLabel = [CCLabelBMFont labelWithString:#"Back" fntFile:#"projectOneMenuItem1.fnt"];
CCMenuItemLabel *back = [CCMenuItemLabel itemWithLabel:backLabel target:self selector:#selector(goBack:)];
CCMenu *menu = [CCMenu menuWithItems: difficulty, audioSource, leaderboards, achievements, back, nil];
menu.position = ccp(winSize.width/2, winSize.height*0.15);
[menu alignItemsVerticallyWithPadding:10];
[self addChild:menu];
You will then need to have a flag that your game can check to decide if it should do any actions or not.
For example if you had your AI doing something in a method update:(ccTime)dt then you would do this:
// Check to see if game actions should be running or not
if(!isGamePaused)
{
// AI Code here
}
You make this flag available to anything that needs it and check for the pause flag. In the code above, isGamePaused is a bool variable available class wide.
If you need to pause a physics simulation like Box2D, this works for that as well.
if(!isGamePaused)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
{
if (b->GetUserData() != NULL)
{
if(!b->IsActive()) continue;
CCSprite *sprite = (CCSprite *)b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
Hopefully that helps.
if i develop ur app i will be do like this
instead of pause the game i will create the custom scene with ur menu.
instead of pausing the game i will do replace the previous scene by this new scene which has already we have added the menu in the previous one.
so it accessible even we pause the game.(pause means faded new scene with ur menu )

UITextField in UIView in UIAlertView not working?

In my iPhone application, If I add uitextfield in uialertview it works fine bt if i add it into uiview and if uiview is added into uialertview it is nt working. Getting strange behiviour?
Please help, If any one know about that! Thank you.
Note: here UItextField not working means- it is not taking any input but interesting is that if click on it keyboard appears, also its delegate are not gets calling.
just add a temp textField in ur alerView below the view of yours
tf = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
[alertView addSubView:tf];
in your alertView and every thing will work good ..... this is just to inform alertView that there is some view in it that need to be first responder at some point of time
i had the same problem. i tried change size of frames. this code isn't really good, but works for me:
- (void)drawRect:(CGRect)rect {
self.bounds = CGRectMake(0, 0, 284, 248);
int i = 0;
for (UIView*classW in self.subviews) {
if ([NSStringFromClass([classW class]) isEqualToString:#"UIThreePartButton"]) {
classW.frame = CGRectMake(11+135*i, 189, 127, 43);
i++;
}
if ([NSStringFromClass([classW class]) isEqualToString:#"UIView"]) {
classW.backgroundColor = [UIColor redColor];
CGRect f = classW.frame;
f.size.height += 30;
classW.frame = f;
}
}
}
I just created a post, which can solve your problem.
Key: You need to add a fake UITextfield via coding behind your view, in order to make your other UITextfields in your customized view to capture inputs from Keyboard. I don't why, but it works.
http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

Move a button in the screen

I have an UIButton and I want to move that button by touching and swiping it in the screen. When I release the touch it will be in the current position. Explain clearly, please.
You can move a view by using touch moved event. There is a sample tutorial MoveMe by Apple which drags a view and after releasing the touch animate the view. Check specially the touch events (touchesBegan, touchesMoved, touchesEnded) in MoveMeView.m to get the idea how they have moved placardView. You can move your button just like the placardView.
Taken from 'drag' move a uibutton so it acts like uiScrollView
check this
you should make move frame from the points and move frame accordingly so that your button moves in places of touches
If you are scripting for iOS 3.2 and above, consider using UIPanGestureRecognizer.
Simply attach an instance of it like this,
...
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
[self.button addGestureRecognizer:panGesture];
[panGesture release];
...
and define handlePan: like this,
- (void)handlePan:(UIPanGestureRecognizer *)panGesture {
CGRect buttonFrame = self.button.frame;
CGPoint translation = [panGesture translationInView:panGesture.view];
buttonFrame.origin.x += translation.x;
buttonFrame.origin.y += translation.y;
[panGesture setTranslation:CGPointMake(0, 0) inView:panGesture.view];
self.button.frame = buttonFrame;
}

UIPopoverController animates when keyboard pops up

Heey,
In my iPad application I have a UIPopoverController with a UIViewController containing some textfields.
When the keyboard comes up, the Popover gets animated to fit. Does anybody know how to disable this?
Thanks
I dont think you can except make the popovers smaller in height...Its done like that so when your keyboard pops up none of the popover gets covered by it (thus it shrinks back), however i have found it to be annoying at times since it messed with table views (not making them able to scroll all the way and having to resize them)
It would be great if it did actually animate to better part of the screen, I think you mean it actually shrinks the popUp which is mostly not good.(which i see during rotation in my case). You can not keep the popUp from squishing, UNLESS you move a view. The best way to handle this is to temporarily move your entire main UIView of the whole screen up with the keyBoard by the difference between the size of your pop up, and how much that pop up would shrink if you did not move it up... you can't just move it up by the size of your keyboard, because popUps up high would then also be effected. This code below is for when the keyboard rotated, similar code for when it is first introduced. easy to do, EXCept for when you rotate the screen, then things get tricky...
in otherwords, sometimes your UIView will not move at all, sometimes it will move up by a good 170 points.
//-----------------finding the keyboard top (also used on "didRotate")----very handy,
// took me hours to figure out an absolute always work topOfKeyboard, so here you go.-------
//--------------------------------------------------------------------------------
- (void)keyboardWillShow:(NSNotification*)notification
{
NSLog(#" keyboardWillShow");
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
NSDictionary* info = [notification userInfo];
keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [(UIView*)keyWindow convertRect:keyboardRect toView:mainViewController.view];
keyboardIsVisible = TRUE;
topOfKeyboard = keyboardRect.origin.y;
the tricky part is finding the PopUp bottom, because there appears to be code in the popup itself or the convertRect:toView: code that makes the origin flaky , (if you try to "view" origin after a "convertRect:toView:" code), it wants to move and be in different spots during rotation,(or one of it's super views) so bottom calc comes out different some times,(not predicable) because of async process of the rotation of different elements possibly because the popUp itself has many superviews down in the pop up. (to see problem in action with pop up and, must have keyboard effecting popup, move the whole view up then log the "origin" and "size" of popUp after the "convertRect:toView:" code)... the "origin" i'm talking about is the origin of the frame after it is translated to the main view (or atleast a couple views up) with the "convertRect:toView:" code....
update:(it appears if you move down about 3 superviews from the popUp, the flakiness goes away... (this code below was in a "didRotate" ipad type of code. That is that the popUp has several superviews before you can get to the one that is projected ontop of the proper frame
UIPopoverController probably should have a property that has the origin that is predicted after a rotation in it, in a type of "will rotate" kind of code, (because of the keyboard problem), instead of just the "popoverContentSize", (also should include the popoverContentSize that would have been without the keyboard as another variable, ".. anyway This is how I had to do it, see code below.
//--------------------------------------------------------------------------------
- (void) checkRotation
{
NSLog(#" ");
NSLog(#"checkRotation");
if (wasOffset)
{
wasOffset = false;
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = carousel.frame;
frame.origin.y += offset;
carousel.frame = frame;
[UIImageView commitAnimations];
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
if (popPickerController.popoverVisible)
{
if (keyboardIsVisible)
{
wasOffset = false;
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview]
permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
upView3 = [[[popPickerController.contentViewController.view superview] superview] superview]; //hey it works... :o)
//NSLog(#" ");
//NSLog(#"upView3.frame.origin.x = %f",upView3.frame.origin.x);
//NSLog(#"upView3.frame.origin.y = %f",upView3.frame.origin.y);
//NSLog(#"upView3.frame.size.height = %f",upView3.frame.size.height);
//NSLog(#"upView3.frame.size.width = %f",upView3.frame.size.width);
//NSLog(#" ");
popUpRect.origin.x = upView3.frame.origin.x;
popUpRect.origin.y = upView3.frame.origin.y;
popUpRect.size.height = popUpSize.height;
popUpRect.size.width = popUpSize.width; //you must save the size because the keyboard destroys it before you can use it. very tricky....
//NSLog(#" ");
//NSLog(#"popUpRect.origin.x = %f",popUpRect.origin.x);
//NSLog(#"popUpRect.origin.y = %f",popUpRect.origin.y);
//NSLog(#"popUpRect.size.height = %f",popUpRect.size.height);
//NSLog(#"popUpRect.size.width = %f",popUpRect.size.width);
//NSLog(#" ");
//NSLog(#" ");
//NSLog(#"keyboardIsVisible = %d", keyboardIsVisible);
//NSLog(#" ");
//NSLog(#"keyboardRect.origin.x = %f",keyboardRect.origin.x);
//NSLog(#"keyboardRect.origin.y = %f",keyboardRect.origin.y);
//NSLog(#"keyboardRect.size.height = %f",keyboardRect.size.height);
//NSLog(#"keyboardRect.size.width = %f",keyboardRect.size.width);
//NSLog(#"topOfKeyboard = %f",topOfKeyboard);
CGFloat bottomOfPicker = popUpRect.origin.y + popUpRect.size.height - amountShadowCanEncroach;
//NSLog(#" ");
//NSLog(#"bottomOfPicker = %f",bottomOfPicker);
//NSLog(#"topOfKeyboard = %f",topOfKeyboard);
//NSLog(#" ");
if (bottomOfPicker > topOfKeyboard)
{
wasOffset = true;
offset = bottomOfPicker - topOfKeyboard;
NSLog(#"offset = %f",offset);
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = carousel.frame;
frame.origin.y -= offset;
carousel.frame = frame;
[UIImageView commitAnimations];
}
}
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
and moving the main UIView back
//-----------------------------------------------------------------------------
- (void) searchDidEndEditing
{
NSLog(#"searchDidEndEditing");
keyboardIsVisible = false;
if (wasOffset)
{
wasOffset = false;
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.2f];
CGRect frame = mainView.frame;
frame.origin.y += offset;
mainView.frame = frame;
[UIImageView commitAnimations];
if (zoneButton.selected)
[popPickerController presentPopoverFromRect:zoneButton.frame inView:[zoneButton superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}