Here is the event:
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
{
[self setupScrollView];
return YES;
}
Any time the device is rotated, I setup my UIScrollView so that what's inside is always "right" side up.
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait)
{
self.scrollView.transform = CGAffineTransformIdentity;
}
else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown)
{
self.scrollView.transform = CGAffineTransformRotate(self.scrollView.transform, M_PI);
}
else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)
{
self.scrollView.transform = CGAffineTransformRotate(self.scrollView.transform, -M_PI/2);
}
else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight)
{
self.scrollView.transform = CGAffineTransformRotate(self.scrollView.transform, M_PI/2);
}
This works except when the device is rotated from landscape back to Portrait (right side up), breakpoints verify that shouldAutorotateToInterfaceOrientation is not being called. So when device is turned back to portrait the UIScrollView contents are sideways. How can I fix this?
Still not sure why the above did not work, but the following seems to work:
- (void)viewDidLoad {
[super viewDidLoad];
//for some reason shouldAutorotateToInterfaceOrientation isn't firing when returning to portrait so we need to register for notifications
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setupScrollView) name:UIDeviceOrientationDidChangeNotification object:nil];
}
Related
I am navigating between SK scenes using the following code:
SKScene *newScene = [[MyScene alloc] initWithSize:self.size state:state];
float duration = 1;
SKTransition *transition = [SKTransition fadeWithDuration:duration];
[self.view presentScene:newScene transition:transition];
Regardless the type of scene I am trying to present, the app crashes every time with EXC_BAD_ACCESS (code 1) without any zombies.. It looks like the "present scene won't work correctly on xcode6, it did work before I upgraded from xcode5
edit: this is the initWithSize code:
-(id)initWithSize:(CGSize)size state:(GameState)state{
if (self = [super initWithSize:size]) {
[self setupScene];
if (state == GameStateLaunch) {
[self setupLoadingScreen];
[self switchToLaunch];
} else if (state == GameStateTutorial || state == GameStateReplay) {
[self switchToTutorial];
} else if (state== GameStateMainMenu) {
[self switchToMainMenu];
}
}
return self;
}
I managed to make it work partially by removing all children before presenting the new scene:
- (void)cleanUpChildrenAndRemove {
for (SKNode *child in self.children) {
[child removeFromParent];
}
[self removeFromParent];
}
However, this is crazy. There must be a better solution!
How to make avplayer in osx autoplay?
I've tried this:
NSURL *videoURL = [[NSBundle mainBundle]
URLForResource:#"video name" withExtension:#"mp4"];
self.movieView.player = [AVPlayer playerWithURL:videoURL];
it doesn't autoplay?
You haven't called the play method.
[self.movieView.player play]
I think, AVplayer first check the URL content is available or not. You must add observer on it.
[avplayerObject addObserver:self forKeyPath:#"status" options:0 context:nil];
#pragma Observer for Player status.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == _audioPlayer && [keyPath isEqualToString:#"status"]) {
if (_audioPlayer.status == AVPlayerStatusFailed) {
NSLog(#"AVPlayer Failed");
loader.hidden = true;
} else if (_audioPlayer.status == AVPlayerStatusReadyToPlay) {
NSLog(#"AVPlayerStatusReadyToPlay");
[_audioPlayer play];
} else if (_audioPlayer.status == AVPlayerItemStatusUnknown) {
NSLog(#"AVPlayer Unknown");
}
[_audioPlayer removeObserver:self forKeyPath:#"status" context:nil];
}
}
I have a TabBarController, supportedInterfaceOrientations - UIInterfaceOrientationMaskPortrait. Navigation bar moves up under the status bar when I start watching video from one of tabs, rotate device to landscape and then exit from fullscreen. Why it's happens and how fix it?
This is a kind of hack, but works in my view controller contains UIWebView and fullscreen videos start from <video> tags.
You will see jerking of navigation bar because UIWindowDidBecomeHiddenNotification is called after video fullscreen window just disappear.
- (void)viewDidLoad
{
[super viewDidLoad];
// NOTE: I'm not sure, but MPMoviePlayerWillExitFullscreenNotification won't work
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)windowDidBecomeHidden:(NSNotification *)notification
{
UIView *navbar = self.navigationController.navigationBar;
CGRect barFrame = navbar.frame;
barFrame.origin.y = 20;
self.navigationController.navigationBar.frame = barFrame;
UIView *navbarBack = nil;
for (UIView *view in navbar.subviews) {
if ([NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"]) {
navbarBack = view;
break;
}
}
CGRect backFrame = navbarBack.frame;
backFrame.origin.y = -20;
backFrame.size.height = 64;
navbarBack.frame = backFrame;
[navbar.superview setNeedsLayout];
}
UIView *navbarBack = [navbar.subviews bk_match:^BOOL(UIView *view) {
return [NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"];
}];
I have met the same issue.I tried to use js to get the "webkitendfullscreen" event,and then found navbar.frameOriginY = 0,which should be 20.
If I push from rootViewController to detailViewController in landscape mode. My ScrollView is not changing the 'y' position. My code:-
- (void)viewDidLoad
{
NSLog(#"DetailViewController viewDidload - Start");
[super viewDidLoad];
// Do any additional setup after loading the view.
[self delayedCheck];
NSLog(#"DetailViewController viewDidload - End");
}
-(void)delayedCheck {
NSLog(#"delayedCheck start");
UIInterfaceOrientation toStatusBarInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (toStatusBarInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toStatusBarInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"Landscape");
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgL.jpg"]];
[self.detailContentScrollView setFrame:CGRectMake(self.detailContentScrollView.frame.origin.x, 170.0, self.detailContentScrollView.frame.size.width, self.detailContentScrollView.frame.size.height)];
} else {
NSLog(#"Portrait");
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgP.jpg"]];
[self.detailContentScrollView setFrame:CGRectMake(self.detailContentScrollView.frame.origin.x, 185.0, self.detailContentScrollView.frame.size.width, self.detailContentScrollView.frame.size.height)];
}
NSLog(#"delayedCheck End");
}
UPDATE
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"willAnimateRotationToInterfaceOrientation");
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"Landscape");
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgL.jpg"]];
[self.detailContentScrollView setFrame:CGRectMake(self.detailContentScrollView.frame.origin.x, 170.0, self.detailContentScrollView.frame.size.width, self.detailContentScrollView.frame.size.height)];
} else {
NSLog(#"Portrait");
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgP.jpg"]];
[self.detailContentScrollView setFrame:CGRectMake(self.detailContentScrollView.frame.origin.x, 185.0, self.detailContentScrollView.frame.size.width, self.detailContentScrollView.frame.size.height)];
}
}
I am trying to change the scrollview 'y' position in landscape mode. I navigate from rootViewController to detailViewController, In detailView - the position of scrollview is not changing.
Please help me out to change the position of 'y' origin of scrollView. Thanks in Advance....
- (void)viewWillLayoutSubviews
{
UIInterfaceOrientation toStatusBarInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (toStatusBarInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toStatusBarInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgL.jpg"]];
}
else
{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgP.jpg"]];
}
}
I placed the viewWillLayoutSubviews method and made changes as mentioned in code.
Am working in iphone application. In my application i need to resize my uitextfield keyboard.
When i click uitextfield the keyboard appear half of the screen. So i need to reduce height of the keyboard. Can any one help me out of this issue?. Thanks in advance.
You are not allowed to resize the keyboard. Apple does not allow it and doing so will lead to app rejection. You should move the view up for the keyboard not to hide textfields. Add these to your viewcontroller.m file and do not forget to conform to UITextFieldDelegate in your .h file:
#define kOFFSET_FOR_KEYBOARD 80.0 //on top of .m file under imports
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:self.passField])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
In this way the view will move up when the keyboard is shown.