Reputation: 7377
Is here a way to run a method when the iOS devices orientation changes?
I would like to change only some objects orientations on the screen, and not others.
What delegates do I use etc.
Cheers -A newbie
Upvotes: 12
Views: 18730
Reputation: 3969
Just use this code snippet to check the orientation changes.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
} else {
print("Portrait")
}
}
Upvotes: 0
Reputation: 4182
My sandbox app: https://github.com/comonitos/programatical_device_orientation
The solution is easy:
in interface (h file) :
BOOL rotated;
in implementation (m file):
rewrite
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return rotated; }
2 call [self setup]
-(void) setup
{
rotated = YES;
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
rotated = NO;
}
Upvotes: 0
Reputation: 53669
UIDevice
posts UIDeviceOrientationDidChangeNotification
UIApplication
posts UIApplicationWillChangeStatusBarOrientationNotification
and UIApplicationDidChangeStatusBarOrientationNotification
and has a related delegate callback for each.
UIViewController
receives several orientation related calls triggered by the UIDevice notification if the view controller is part of the controller hierarchy managed by a window.
If you are already using a UIViewController, implement some of the orientation related methods, otherwise register for the UIDevice notifications. The most important UIViewController method is shouldAutorotateToInterfaceOrientation
because if that return NO
the others are not called.
Upvotes: 6
Reputation: 77400
UIViewController
s are sent willRotateToInterfaceOrientation:duration:
just before rotation, and didRotateFromInterfaceOrientation:
after rotation.
To configure additional animations, use either willAnimateRotationToInterfaceOrientation:duration:
or willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
, didAnimateFirstHalfOfRotationToInterfaceOrientation:
and willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
. The latter are used for two-step animations, which you generally use when you have header or footer views that are moved offscreen for the main transition animation.
Upvotes: 0
Reputation: 7774
Depends when you want to react:
If before rotation, override from UIViewController:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// do something before rotation
}
If you want to perform something after rotation:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// do something after rotation
}
Reference:
Upvotes: 31
Reputation: 10312
willRotateToInterfaceOrientation: is the method you are possibly looking at. Read up on that one.
Upvotes: 0