ipad app - most views rotate ok. One view does not - xcode

I am building an iPad app where all views should autorotate. One view is giving me a headache.
I know that variants of this question have been asked a couple of times before, but I cannot find a working solution..
I flip to this view by calling a routine in the app. delegate. The code for this routine is:
- (void)flipToBack {
//NSLog(#"%s", __FUNCTION__);
DrawingViewController *drawView = [[DrawingViewController alloc] initWithNibName:#"Draw" bundle:nil];
[self setDrawingViewController:drawView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[self.window addSubview:[drawView view]];
[UIView commitAnimations];
// NSLog (#" FINISHED ");
}
This is a view controller (mvc) with a UIView subview, the latter, where drawing takes place. Neither the mvc nor the subview are rotating properly.
The code I'm using to invoke the rotation in the mvc is:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
The mvc has two tool bars, top and bottom, and these need to resize. These should also stay top and bottom regardless of the rotation.
The project summary shows all orientations are supported, but I think that this is just for app. initialization.
I have all struts on for the mvc.
I'm building the toolbars like this:
UIInterfaceOrientation fieldOrient = [[UIApplication sharedApplication] statusBarOrientation];
if ((fieldOrient == UIDeviceOrientationLandscapeLeft) || (fieldOrient == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 50, 1024, 35 );
topToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 50, 768, 35 );
topToolbar.frame = frame;
UIInterfaceOrientation fieldOrient = [[UIApplication sharedApplication] statusBarOrientation];
if ((fieldOrient == UIDeviceOrientationLandscapeLeft) || (fieldOrient == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 650, 1024, 35 );
bottomToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 950, 768, 35 );
bottomToolbar.frame = frame;
I guess I'm missing something obvious.. Any help/pointers would be appreciated.

Try setting the frame in the will rotate to interface orientation method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ((toInterfaceOrientation == UIDeviceOrientationLandscapeLeft) || (toInterfaceOrientation == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 650, 1024, 35 );
bottomToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 950, 768, 35 );
bottomToolbar.frame = frame;
}

The answer for me was to present the view as a modal view. It's not ideal but it works.

Related

Slide menu with storyboards ios 7

I need to implement on my app a Slide menu (like the one on the facebook app) but i also wants to have buttons on my Views that links to other Views, for example on my Main View I have a button that on click goes to OptionView but i also want to have a slide menu That links to OptionView, i'm using storyboards and xcode 5 and I wanted to know if someone can help me to figure out how to create the Slide menu
You can either use this to have an idea / use the open source:
https://github.com/aryaxt/iOS-Slide-Menu
or you can implement it like this:
Add to your navigation bar a UIBarButtonItem:
UIBarButtonItem *rightRevealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"reveal-icon.png"] style:UIBarButtonItemStyleBordered target: self action:#selector(rightRevealToggle:)];
self.navigationItem.rightBarButtonItem = rightRevealButtonItem;
implement the rightRevealToggle:
- (void)rightRevealToggle:(id)sender
{
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
self.navigationItem.rightBarButtonItem.enabled = FALSE;
CGRect rearViewFrame = _rearViewController.view.frame;
float deltaToMoveRearView = _rearViewController.datePicker.frame.size.width;//0.25f * width;
//slide the rear view inside
if(_rearViewController.view.hidden && ![sender isKindOfClass:[UISwipeGestureRecognizer class]])
{
_rearViewController.view.hidden = FALSE;
[UIView animateWithDuration:0.7f animations:^{
_rearViewController.view.frame = CGRectMake(width - deltaToMoveRearView,rearViewFrame.origin.y,rearViewFrame.size.width,rearViewFrame.size.height);
_contentViewController.view.frame = CGRectMake(-deltaToMoveRearView , 0, width, _contentViewController.view.frame.size.height);
NSLog(#"3: frame: x:%f y:%f",_contentViewController.view.frame.origin.x,_contentViewController.view.frame.origin.y);
} completion:^(BOOL finished){
self.navigationItem.rightBarButtonItem.enabled = TRUE;
}];
}
//slide the rear view outside
else
{
self.rearViewController.view.hidden = FALSE;
[UIView animateWithDuration:0.7f animations:^{
_rearViewController.view.frame = CGRectMake(width,rearViewFrame.origin.y,rearViewFrame.size.width,rearViewFrame.size.height);
_contentViewController.view.frame = CGRectMake(0, 0, width, _contentViewController.view.frame.size.height);
self.titleLabel.alpha = 1;
} completion:^(BOOL finished){
_rearViewController.view.hidden = finished;
self.navigationItem.rightBarButtonItem.enabled = TRUE;
}];
[self updateContentViewController];
}
}
- (void)updateContentViewController
{
UIViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"%#VC",_rearViewController.selectedSection]];
if([newVC class] == [ScoresViewController class])
{
if([_contentViewController class] == [ScoresViewController class] &&
[((ScoresViewController *) _contentViewController) date] != _rearViewController.datePicker.date)
{
((ScoresViewController *) newVC).date = _rearViewController.datePicker.date;
[self cycleToViewController:newVC];
}
}
if([newVC class] != [_contentViewController class])
{
[self cycleToViewController:newVC];
}
}
see the below link with so many examples
Code4App Menu

how can I make a scrollView autoscroll?

I have this scrollView:
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.scrollView.contentSize = CGSizeMake(320,3000);
_scrollView.frame = CGRectMake(0, 45, 320, 420);
and I want to make it autoscroll very slowly downward to the end so that the user can see the content (as in movie credits), eventually with a button to stop/play, but to follow the user gestures when touching the interface.
How can I do this?
Thanks,
You can use:
[_scrollView setContentOffset:CGPointMake(x,y) animated:YES];
and use the x and y as the touch points on the screen you can capture.
You can also do an animation with CoreAnimation:
[UIScrollView beginAnimations:#"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:1.0f];
[scroll setContentOffset:CGPointMake(x, y)];
[UIScrollView commitAnimations];
this adapted code did the trick (source http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)
PS : each image is 280 by 200
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=10; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.jpg",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.y;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
// if page is not 10, display it
if( nextPage!=10 ) {
[scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
You can set x if you want to scroll horizontally, otherwise set y to scroll vertical.
[_scrollView setContentOffset:CGPointMake(x, y) animated:YES];
and modify the co-ordinates accordingly.

Cocos2D: openGL-es becoming transparent over UIView

For reasons outside the capabilities of Cocos2D, I needed to make it transparent and show UIView's behind it.
This works fine except under a certain circumstance. When a sprite's opacity becomes more transparent, it makes everything else in Cocos2D drawn under it become transparent for some reason which makes my UIViews show through.
Shown here on the first image is water tiles, drawn in Cocos2D with a UIView of the mountain and the sky showing under it. The second image is the same thing except in Cocos2D there's a black sprite covering the whole screen at half opacity, which for some reason, makes the UIView under it show through. Where this is most visible in my example is the mountain showing through the water tiles.
My delegate's applicationDidFinishLaunching code is as follows:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeDefault];
CCDirector *director = [CCDirector sharedDirector];
// Init the View Controller
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8, kEAGLColorFormatRGB565
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
// attach the openglView to the director
[director setOpenGLView:glView];
glView.opaque = NO;
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
#else
[director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
#endif
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
//color/gradient BG
bgLayer = [ColorBGView createGradientWithName:#"darkCave"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
bgLayer.frame = CGRectMake(0, 0, screenHeight, screenWidth);
[viewController.view.layer insertSublayer:bgLayer atIndex:0];
// add custom parallax view
parallaxView = [[ParallaxView alloc] init];
parallaxView.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view insertSubview:parallaxView atIndex:1];
[parallaxView release];
// make the OpenGLView a child of the view controller
[viewController.view insertSubview:glView atIndex:2];
viewController.view.opaque = YES;
viewController.view.backgroundColor = [UIColor blackColor];
// make the View Controllers childs of the main window
window.rootViewController = viewController;
foregroundLabelView = [[ForegroundLabelView alloc] init];
foregroundLabelView.frame = CGRectMake(0, 0, 1024, 768);
[viewController.view insertSubview:foregroundLabelView atIndex:3];
foregroundLabelView.hidden = YES;
[foregroundLabelView release];
[window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888]; //
[glView setMultipleTouchEnabled:YES];
// Removes the startup flicker
[self removeStartupFlicker];
// Run the intro Scene
[[CCDirector sharedDirector] runWithScene: [BlankScene scene]];
}
Please let me know if you need anything else. Thank you.

Keyboard behavior when forced open with inputAccessoryView?

I have a device connected to my iPad that is acting as an external keyboard, however, I still need a soft keyboard in my program. I have had some luck using the following article: Show iphone soft keyboard even thought a hardware keyboard is connected, but iOS 5 has broken this method. The bottom of the soft keyboard is slightly cut off in most views, but others only half the keyboard appears inside the window and not where you would expect it to.
All of the following code works in my AppDelegate.m file in iOS4, but not in iOS5.
forceKeyboard is called after UITextFieldTextDidBeginEditingNotification is sent.
-(void) textFieldBegan: (NSNotification *) theNotification
{
UITextField *theTextField = [theNotification object];
theTextField.inputAccessoryView = inputAccessoryView;
[self performSelector:#selector(forceKeyboard) withObject:nil afterDelay:0];
}
//Change the inputAccessoryView frame
-(void) forceKeyboard
{
inputAccessoryView.superview.frame = CGRectMake(0, 502, 1024, 265);
int movementDistance = 1;
float movementDuration = 0.3f;
[UIView beginAnimations: #"kb_anim_open" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
inputAccessoryView.superview.frame = CGRectOffset(inputAccessoryView.superview.frame, 0, movementDistance);
[UIView commitAnimations];
}
Close Keyboard is called after UITextFieldTextDidEndEditingNotification is sent.
-(void) textFieldEnd: (NSNotification *) theNotification
{
UITextView *theTextView = [theNotification object];
[theTextView resignFirstResponder];
[self performSelector:#selector(forceCloseKeyboard) withObject:nil afterDelay:0];
}
-(void) forceCloseKeyboard
{
inputAccessoryView.superview.frame = CGRectMake(0, 502, 1024, 265);
int movementDistance = 502;
float movementDuration = 0.3f;
[UIView beginAnimations: #"kb_anim_close" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
inputAccessoryView.superview.frame = CGRectOffset(inputAccessoryView.superview.frame, 0, movementDistance);
[UIView commitAnimations];
}
Any ideas on how I can tame a soft keyboard would be greatly appreciated; I've tried wrestling with the superview.frame values for awhile but with no luck.
Thanks for reading.
UPDATE
I have figured out a setup that opens and closes the keyboard in iOS 4 and 5, but does not animate. Again, this is all in the AppDelegate where "textFieldBegan" and "textFieldEnd" are called by notifications sent when a user begins and ends editing respectively.
-(void) textFieldBegan: (NSNotification *) theNotification
{
UITextField *theTextField = [theNotification object];
if (!inputAccessoryView) {
inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 1)];
}else{
inputAccessoryView.frame = CGRectMake(0, 0, 1024, 1);
}
theTextField.inputAccessoryView = inputAccessoryView;
[self performSelector:#selector(forceKeyboard) withObject:nil afterDelay:0];
}
-(void) textFieldEnd: (NSNotification *) theNotification
{
NSString *classstr = NSStringFromClass([[theNotification object] class]);
UITextView *theTextView = [theNotification object];
[self performSelector:#selector(forceCloseKeyboard) withObject:nil afterDelay:0];
[theTextView resignFirstResponder];
}
//Change the inputAccessoryView frame
-(void) forceKeyboard
{
//default center = 512, 591
inputAccessoryView.superview.frame = CGRectMake(0, 415, 1024, 353);
[UIView animateWithDuration:0.3 animations: ^ { [inputAccessoryView.superview transform]; }];
}
-(void) forceCloseKeyboard //Unnecessary?
{
inputAccessoryView.frame = CGRectMake(0, 0, 1024, 0);
[UIView animateWithDuration:0.3 animations: ^ { [inputAccessoryView transform]; }];
}
It looks as if the "forceCloseKeyboard" method isn't really doing anything as it looks like when the textField resigns responder it closes the keyboard anyway. I leave it here in case someone can come up with a way to animate the keyboard opening and closing.

Xcode: Why does my flip animation flip twice?

I'm having a little problem. I have an UILabel which have an UILongPressGestureRecognicer. When the UILongPressGestureRecognizer is called my app is supposed to switch to a new view using a flip animation.
This is the code I have used for the GestureRecognizer:
UILongPressGestureRecognizer *labelLongPressRecognizer;
labelLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LoadLabelSettings:)];
labelLongPressRecognizer.numberOfTouchesRequired = 1;
labelLongPressRecognizer.minimumPressDuration = 2.0;
[NewLabel addGestureRecognizer:labelLongPressRecognizer];
and this is the code for the view switching animation:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[self.view addSubview:LabelSettingsViewController.view];
[UIView commitAnimations];
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
LabelSettingsViewController.view.frame = CGRectMake(0, 0, 480, 300);
}
My problem is that when I hold down on my UILabel the switch animation begins, but when I release it repeats the animation again. So basically the animation occur twice and I only want it to take place once.
Any ideas?
Thanks in advance :)
Are you checking the sender state, e.g.,
- (void)LoadLabelSettings:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) // or whatever
// then do the flipping stuff
}
Check out the "Overview" of the "UILongPressGestureRecognizer Class Reference", which talks about the long press being continuous and I presume numerous events could be triggered:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UILongPressGestureRecognizer_Class/Reference/Reference.html

Resources