Reputation: 3045
I am new at Swift. I want to detect device is rotating (portrait or Landscape). And of course I need that in Swift. Objective C code:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
-(void) detectOrientation {
if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) ||
([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
[self doLandscapeThings];
} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
[self doPortraitThings];
}
}
Thank you.
Upvotes: 3
Views: 3950
Reputation: 13842
To detect rotation you can implement these two methods:
override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
}
Translating that code into swift is something you're gonna have to try yourself, with a basic knowledge of swift that shouldn't be a problem. If you have specific issues, you can always post them here on SO, but we can't do your homework for you :-)
(Hint: this is one line of your code:
if UIDevice.currentDevice().orientation == .LandscapeLeft {
}
)
Upvotes: 4