ios开发 present 出来的viewcontroller 强制横竖屏切换代码分享 (代码不解释)


添加下面代码到 控制器中



@property(nonatomic,assign)BOOL isPortraitMode;

@synthesize isPortraitMode=_isPortraitMode;


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
        if (_isPortraitMode) {
            return UIInterfaceOrientationPortrait;
        }else{
            return UIInterfaceOrientationLandscapeRight;
        } 
}

-(NSUInteger)supportedInterfaceOrientations
{ 
        if (_isPortraitMode) {
            return UIInterfaceOrientationMaskPortrait;
        }else{ 
            return UIInterfaceOrientationMaskLandscape;
        } 
}

-(BOOL)shouldAutorotate
{   
if (_isPortraitMode) {
        return NO;
    }else{
        return YES; 
    }
}

-(void)setIsPortraitMode:(BOOL)isPortraitMode{
    DLog();
    _isPortraitMode=isPortraitMode;
    __strongUIViewController * strSelf = self;
    __strongUIViewController   * strFatherViewController =self.presentingViewController;
    [strFatherViewController dismissViewControllerAnimated:NOcompletion:nil];
    [strFatherViewController presentViewController:strSelfanimated:NOcompletion:nil]; 
    if (isPortraitMode==YES) {
        self.view.bounds=CGRectMake(0,0, Dev_ScreenWidth,Dev_ScreenHeight);
    }else{
        //
        self.view.bounds=CGRectMake(0,0, Dev_ScreenHeight,Dev_ScreenWidth);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中的OTG(On-The-Go)功能是指通过Lightning接口或USB-C接口,使iOS设备能够连接外部USB设备,如USB闪存驱动器、键盘、鼠标等。下面是一个简单的iOS开发OTG代码示例: 首先,需要导入ExternalAccessory框架,并在Info.plist文件中添加相应的权限描述。 ```swift import ExternalAccessory class ViewController: UIViewController, EAAccessoryDelegate, EASessionDelegate { var session: EASession? var accessory: EAAccessory? var inputStream: InputStream? var outputStream: OutputStream? override func viewDidLoad() { super.viewDidLoad() // 检测已连接的外部设备 NotificationCenter.default.addObserver(self, selector: #selector(accessoryConnected), name: NSNotification.Name.EAAccessoryDidConnect, object: nil) EAAccessoryManager.shared().registerForLocalNotifications() // 连接指定的外部设备 if let connectedAccessories = EAAccessoryManager.shared().connectedAccessories { for accessory in connectedAccessories { if accessory.protocolStrings.contains("com.yourprotocol") { self.accessory = accessory self.openSession() break } } } } @objc func accessoryConnected(notification: NSNotification) { if let accessory = notification.userInfo?[EAAccessoryKey] as? EAAccessory { if accessory.protocolStrings.contains("com.yourprotocol") { self.accessory = accessory self.openSession() } } } func openSession() { guard let accessory = self.accessory else { return } self.session = EASession(accessory: accessory, forProtocol: "com.yourprotocol") guard let session = self.session else { return } self.inputStream = session.inputStream self.outputStream = session.outputStream self.inputStream?.delegate = self self.outputStream?.delegate = self self.inputStream?.schedule(in: .current, forMode: .default) self.outputStream?.schedule(in: .current, forMode: .default) self.inputStream?.open() self.outputStream?.open() } // 发送数据 func sendData(data: Data) { guard let outputStream = self.outputStream else { return } let bytesWritten = outputStream.write((data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count), maxLength: data.count) if bytesWritten == -1 { // 发送失败 } else if bytesWritten < data.count { // 发送未完成 } else { // 发送成功 } } // 接收数据 func stream(_ aStream: Stream, handle eventCode: Stream.Event) { if eventCode == .hasBytesAvailable { guard let inputStream = self.inputStream else { return } var buffer = [UInt8](repeating: 0, count: 1024) let bytesRead = inputStream.read(&buffer, maxLength: buffer.count) if bytesRead > 0 { let receivedData = Data(bytes: buffer, count: bytesRead) // 处理接收到的数据 } } } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.inputStream?.close() self.outputStream?.close() self.inputStream?.remove(from: .current, forMode: .default) self.outputStream?.remove(from: .current, forMode: .default) self.inputStream?.delegate = nil self.outputStream?.delegate = nil self.inputStream = nil self.outputStream = nil self.session?.close() self.session = nil } } ``` 这是一个简单的iOS开发OTG代码示例,其中包括了连接外部设备、发送数据和接收数据的基本操作。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值