Cocos 2d CCmenu not appearing - menu

I have recently coded in a menu item, the "Game Over" Label appears but the menu item "Restart" isn't. I have no idea why?
Can anyone help, here is whats in my method !
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Game Over"
fontName:#"Marker Felt"
fontSize:64];
label.color = ccRED;
label.position = ccp(winSize.width*0.5, winSize.height*0.75);
[self addChild:label];
CCMenuItem *item = [CCMenuItemFont itemWithString:#"Restart"
target:self
selector:#selector(restartGame)];
CCMenu *menu = [CCMenu menuWithItems:item, nil];
[menu alignItemsVertically];
[self addChild:menu];

try this code:
menu.position = ccp(winSize.width/2, winSize.height/2);
set the specific position.

Related

UITextView scroll performance for large attributed strings

How can scroll performance of a UITextView be improved when using attributed strings? The following sample code results in completely unacceptable performance on an iPad 3.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.lineSpacing = 14.0;
pStyle.lineHeightMultiple = 20;
pStyle.maximumLineHeight = 20.0;
pStyle.minimumLineHeight = 20.0;
UIFont *font = [UIFont systemFontOfSize:20.0];
NSDictionary *attributes = #{ NSFontAttributeName : font, NSParagraphStyleAttributeName : pStyle};
NSMutableAttributedString *newline = [[NSMutableAttributedString alloc] initWithString:#"\n\n"];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:#"" attributes:#{}];
for (int paragraph=0; paragraph<300; paragraph++){
for (int word=0; word<100; word++){
[string appendAttributedString:[[NSAttributedString alloc] initWithString:#"text!"]];
}
[string appendAttributedString:newline];
}
[string setAttributes:attributes range:NSMakeRange(0, string.length)];
self.textView.attributedText = string;
I should mention that I am open to using Text Kit, but I wouldn't know where to start to ensure that performance is ultimately ok. Also, here's what's going on in the profiler while scrolling.
Two things help here.
First, I only needed the lineSpacing style set. Setting any of the other paragraph parameters results in slow scrolling.
The second was to call [self.textView.layoutManager ensureLayoutForCharacterRange:NSMakeRange(0, self.textView.attributedText.length)]; in viewDidAppear:. Otherwise the first time you scroll through the document it is jerky.

pass selected object to second viewcontroller and display in uilabel

I'm attempting to pass a selected item in a tableview to be displayed in a UILabel on a second viewcontroller. Currently I can segue to the second viewcontroller but nothing displays. I've also attempted to create an instance of the selected item for it to be used as reference in a predicate. The segue I have is:
([segue.identifier isEqualToString:#"showResults"]); {
ResultsVC *rvc = (ResultsVC *) [segue destinationViewController];
NSIndexPath *indexPath = [self.searchTV indexPathForSelectedRow];
self.selectedItem = [_fetchedResultsController objectAtIndexPath:indexPath];
rvc.item = self.selectedItem;
rvc.managedObjectContext = self.managedObjectContext;
and in cell for row at index path:
Item *item = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = item.itemName;
and in the second viewcontroller call the label to display the item:
self.itemNameLabel.text = _selectedItem.itemName;
I have tried self.selectedItem.itemName and other iterations with no luck.
What have I missed to get the UILabel to display data?
Thanks for the help!
Use NSUserDefaults to save the selected text and then retrieve it from the other view.
See this post to a similar question.

iPhone - button within cell to push a new view onto screen

Hey guys, I appear to have run into a problem. I have an application (View-based) that has a UITableView displayed on the bottom half of the screen, with the selection of a cell then bringing up a custom cell taking up the bottom of the screen. I have a "more info" button on the bottom right of this cell, and and when it is selected, I wish for it to open a new NIB file, however the only thing I can manage to do is remove the tableView from the screen..I am not sure of what to use before the "addSubview" because it is not self.view which I thought it would be.
- (void)moreInfoButton:(id)selector{
NSLog(#"Button Pressed");
MoreInfo *mivc = [[MoreInfo alloc] initWithNibName:#"MoreInfo" bundle:nil];
[self.tableview removeFromSuperview];
//[self.view addSubview:(UIView *)mivc];
[self.navigationController pushViewController:mivc animated:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List of Events" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[mivc release];
}
Also, the (UIView *) doesn't do the trick either. Any suggestions?
P.S. The UITableView does not use a navigationController at all, it is just the table, would this be the problem?
Also, what if I chose to just push a new view, and not use the navigationViewController for this view, is this possible?
I can't answer your navigation controller question without seeing more of your program. To answer your other question, without using a navigation controller you can just add a new view with:
newViewVC = [[NewViewVC alloc]initWithNibName:#"NewViewVC" bundle:nil];
[self.view addSubview:newViewVC.view];
Typically newViewVC is declared in your .h so you can release it later.
I have found an easy way: pushing it as a modal.
- (void)moreInfoButton:(id)selector{
NSLog(#"Button Pressed");
MoreInfo *mi= [[MoreInfo alloc] initWithNibName:#"MoreInfo" bundle:nil];
[self presentModalViewController:mi animated:YES];
}
All that is needed is a back button in the new nib file.

how can I set contentoffset and change the height of UIScrollview at same time

I'm trying to scroll up my UISCrollView when the keyboard is shown
I use setContentOffset to shift the uiview up.
At the same time I want to decrease the height of my UISCrollView to (view height - keyboard height), so that the entire content view can be scrolled.
I apply both the changes in keyboardWillShow notification
When I actually bring the keyboard up in my app, the contents first get pushed up and then they are pushed down (which gives a flickering effect). I'm trying to smooth out both the transformations in one go..
Is it possible?
Code Below ---
- (void) keyboardWillShow {
CGPoint contentOffset = scrollView.contentOffset;
CGRect scrollViewFrame = scrollView.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
if (contentOffset.y >= 0 && contentOffset.x >= 0) {
isContentOffset = YES;
contentOffset.y += screenShift;
[scrollView setContentOffset:contentOffset animated: NO];
}
scrollViewFrame.size.height = keyboardOrigin.y - self.view.frame.origin.y - toolbarHeight;
scrollView.frame = scrollViewFrame;
[UIView commitAnimations];
}
There is an option for animation when you setContentOffset for animation. here is the code that i use all the time
- (void)textViewDidBeginEditing:(UITextView *)textView
{
svos = scrollView.contentOffset;
CGRect rect = [textView bounds];
rect = [textView convertRect:rect toView:self.scrollView];
CGPoint point = rect.origin ;
point.x = 0 ;
[self.scrollView setContentOffset:point animated:YES];
doneButton.enabled = YES;
}
- (IBAction)donePressed
{
[scrollView setContentOffset:svos animated:YES];
[textview resignFirstResponder];
doneButton.enabled = NO;
}
It works fine for me.
Are you wrapping these changes in an animation block?
[UIView beginAnimations:#"resizeScroll" context:nil];
// make your changes to set and content offset
[UIView commitAnimations];
I think I have got a solution for this. You can handle the resize of the view in textViewDidBeginEditing method. You can just change the frame size of the scrollView to half by
CGRect tframe = myscrollView.frame;
tframe.size.height/=2;
myscrollView.frame = tframe;
and to initialize or handle the total length of the scrollview you can set the contentSize of the scrollView in the similar fashion as the frame was set in above snippet. for example
CGSize tsize = self.view.frame.size;
//here you can change the height and width of the scrollView
myscrollView.contentSize = tsize;
I hope this should do the trick for you. If it does please communicate.
Figured out the issue...
When the keyboard button was clicked, I was doing a becomeFirstResponder followed by a resignFirstResponder on keyboard view. This caused keyboardWillShow followed by keyboardWillHide and another keyboardWillShow notification, which brought the keyboard up, brought it back down and then up again.
Thanks to everybody who tried to help out.. and sorry the issue was pretty different...

uiwebview zoom programmatically

I want to change the UIWebView zoom level programmatically.
I have tried these but they are not worked for me ;
UIScrollView *sv = [theWebView.subviews objectAtIndex:0];
[sv setZoomScale:50 animated:YES];
also,
[theWebView stringByEvaluatingJavaScriptFromString:#"document.body.style.zoom = 5.0;"];
Any idea?
This is the only way I could find to accomplish it:
(specify your own sizes if you wish, i was attempting to zoom out after typing into a form field)
UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];
I would use:
UIScrollView * sv = self.theWebview.scrollView;
[sv setZoomScale:50];
where self is the viewController containing the webView.
Your problem could be the size of your zoomScale exceeds the size of sv.maximumZoomScale.
UIScrollView * sv = yourwebview_outlet.scrollView;
[sv setZoomScale:50];

Resources