[英]Rotate the view only in ios
我的應用程序僅在縱向模式下受支持。 我要求僅根據設備方向旋轉特定的UIView
。
你能幫我么?
在您的viewdidload或init中添加以下代碼
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
在deviceOrientationDidChange函數中,您將獲得設備方向,據此您可以更新視圖
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Do my thing
}
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
// set your view frame work for portrait mode
}
else {
// set new frame of view for landscape mode.
}
希望你能明白。 謝謝
經過長時間的嘗試,我使用CGAffineTransform
找到了答案。
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CGFloat angle;
switch (orientation) {
case UIDeviceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIDeviceOrientationLandscapeRight:
angle = - M_PI_2;
break;
case UIDeviceOrientationLandscapeLeft:
angle = M_PI_2;
break;
case UIDeviceOrientationPortrait:
default:
angle = 0.f;
break;
}
animationView.transform= CGAffineTransformMakeRotation(angle);
animationView.frame = self.view.frame;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.