Sha*_*oon 8 objective-c rotation uiview ios
我有一个UIView以编程方式创建的:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];
UIImageView* closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"modal_close.png"]];
closeButton.frame = CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09, closeButton.frame.size.width, closeButton.frame.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09,20, 20)];
[button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*.1,self.frame.size.height*.1,self.frame.size.width*.8,self.frame.size.height*.8)];
mainView.backgroundColor = [UIColor whiteColor];
_displayMainView = mainView;
[self addSubview:_displayMainView];
[self addSubview:closeButton];
[self addSubview:button];
mainView = nil;
closeButton = nil;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
如何检测旋转?这是作为另一个现有视图之上的模态.这是一款仅限iPad的应用,如果重要的话.
小智 42
您可以让设备开始生成轮换通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)
添加您自己的选择器作为设备轮换通知的观察者:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
Run Code Online (Sandbox Code Playgroud)
然后编写通知处理程序
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtain current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Do my thing
}
Run Code Online (Sandbox Code Playgroud)
记得在调用自定义UIView的dealloc方法时(或者在完成时)删除观察者,并停止生成设备更改通知.
-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
Run Code Online (Sandbox Code Playgroud)
这对你的问题有帮助吗?
我正在重写 @clearwater82 对 Swift 3 的回答,因为他的方法是我最终使用的方法:
我在视图的init方法中添加了以下代码(对于控制器,它将在它的viewDidLoad方法中):
// Handle rotation
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(
self,
selector: #selector(self.orientationChanged(notification:)),
name: NSNotification.Name.UIDeviceOrientationDidChange,
object: nil
)
Run Code Online (Sandbox Code Playgroud)
然后我将该orientationChanged方法添加到我的视图中。您可以根据需要重命名此方法,只需记住在上面的代码片段中重命名它:
// Called when device orientation changes
@objc func orientationChanged(notification: Notification) {
// handle rotation here
}
Run Code Online (Sandbox Code Playgroud)
最后,我添加了deinit删除视图时删除不必要通知的方法:
deinit {
NotificationCenter.default.removeObserver(self)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
Run Code Online (Sandbox Code Playgroud)
最后一个片段的作用是从通知观察者中删除视图本身,然后阻止设备生成其他旋转通知。这种方法正是我所需要的,但您可能不想在删除视图时停止生成通知。
我希望这是有用的,干杯
我认为你有两个选择:
UIViewController并重写该shouldAutorotateToInterfaceOrientation:方法,以便为您想要支持的所有方向返回 YES。视图控制器将自动旋转内容(您的视图)并调整其大小。您只需确保具有正确的 autoresizeMask (或约束,如果您使用自动布局)。如果可以的话,这确实是您应该选择的第一种方法。
| 归档时间: |
|
| 查看次数: |
19155 次 |
| 最近记录: |