提高生產力的 iPad OS

目錄

  • 概況

    • 文本編輯
    • 鍵盤(鍵盤浮動、拆分、速滑輸入)
    • 多任務處理功能
    • 拖放
    • 整頁截取標記
    • Safari 瀏覽器
  • 基於 iPadOS 進行開發

    • 將 iPad app 帶入 Mac
    • Slide Over and Split View (水平滑動和拆分視圖)
    • Drag and Drop (拖放)
    • Picture in Picture (畫中畫)
  • 拓展


一、概況
1.1、文本編輯
  • 三指捏和拷貝
  • 三指下放粘貼
  • 三指向左輕掃撤銷操作
  • 光標導航、 優化文本選取、智能文本選擇
1.2、鍵盤(鍵盤浮動、拆分、速滑輸入)
1.3、多任務處理(“設置”>“主屏幕與程序塢”>“多任務處理”)
  • 側拉打開App
  • 分屏瀏覽同時使用兩個應用
  • 在應用之間拖放
1.4、整頁截取標記
1.5、Safari 瀏覽器
  • 請求移動網站
  • 請求桌面站點

二、基於 iPad OS 進行開發
  • 2.1、將 iPad app 帶入 Mac
- 需要重新生成證書
- 有太多iPad的痕跡,可能部分頁面需要修改
- 佈局方式
- 數據共享
- 代碼共享
- 應用喚醒、通訊
- 系統開啓:設置->主屏幕與程序塢->允許多個App

代碼配置:
- 提供LaunchScreen.storyboard啓動文件
- Info.plist做出如下相關配置

參考資料 - 史上第二走心的 iOS11 Drag & Drop 教程
參考資料 - WWDC 2017 iOS11 新特性 Drag and Drop 解析
參考資料 - Drag & Drop 改變了我對 iOS 的看法

  • Drag
- UIDragInteractionDelegate
- UITableViewDragDelegate
- UICollectionViewDragDelegate
- UITextDragDelegate
// 在添加 UIDragInteraction 對象之後,就具備了可被 Drag 的行爲
UIDragInteraction *dragInter = [[UIDragInteraction alloc] initWithDelegate:self];
[self.firstImageView addInteraction:dragInter];
self.firstImageView.userInteractionEnabled = YES;

#pragma mark - UIDragInteractionDelegate
// 單指長按某個 View 時,如果添加了 UIDragInteraction,Drag 即刻啓動,進入 itemsForBeginningSession 的回調
- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session {
    /**
     1、UIDragInteraction 可以包含多個 UIDragSession
     2、每個 UIDragSession 又可以包含多個 UIDragItem
     3、UIDragItem 則是 Drop 時接收方所受到的對象
     */
    return [self itemsForSession:session];
}

// 如何生成 UIDragItem 呢
- (NSArray*)itemsForSession:(id<UIDragSession>)session {
    NSItemProvider *provider = [[NSItemProvider alloc] initWithObject:self.firstImageView.image];
    UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:provider];
    dragItem.localObject = self.firstImageView.image;
    return @[dragItem];
}
  • Drop
- UIDropInteractionDelegate
- UITableViewDropDelegate
- UICollectionViewDropDelegate
- UITextDropDelegate
UIDropInteraction *dropInteraction = [[UIDropInteraction alloc] initWithDelegate:self];
[self.secondImageView addInteraction:dropInteraction];
self.secondImageView.userInteractionEnabled = YES;

#pragma mark - UIDropInteractionDelegate
- (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session{
    if(session.localDragSession == nil){
        // 如果是來自於外部 App 的 Drag,localDragSession 爲 nil
        return YES;
    }
    return [session canLoadObjectsOfClass:[UIImage class]];
}

- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session{
    /**
     如果禁止外部app拖動到這裏的話直接 UIDropOperationCancel 就行了
    
     UIDropOperation opera = session.localDragSession ? UIDropOperationCopy : UIDropOperationCancel;
     return [[UIDropProposal alloc] initWithDropOperation:opera];
     */
    return [[UIDropProposal alloc] initWithDropOperation:UIDropOperationCopy];
}

- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session{
    // performDrop 中的操作最好是採用異步的方式,任何費時的操作都會導致主線程的卡
    [session loadObjectsOfClass:[UIImage class]
                     completion:^(NSArray<__kindof id<NSItemProviderReading>> * _Nonnull objects) {
        for (id object in objects) {
            UIImage *image = (UIImage*)object;
            if (image) {
                // handle image
                self.secondImageView.image = image;
            }
        }
    }];
}
- 系統開啓“畫中畫”:設置->主屏幕與程序塢->多任務->畫中畫
- SDK支持:AVKit,AV Foundation、WebKit類進行播放
- AVKit->AVPlayerViewController默認開啓PiP,如果想要關閉添加如下代碼
if (@available(iOS 9.0, *)) {
    _avPlayerController.allowsPictureInPicturePlayback = NO;
}

- WebKit->WKWebView 默認開啓畫中畫,如果想關閉直接添加如下代碼
if (@available(iOS 9.0, *)) {
    _appConfiguration.allowsPictureInPictureMediaPlayback = NO;
}

三、拓展

  • 3.1、遇到的問題:實用WKWebView播放優酷視頻鏈接失敗
    原因:iPad OS 在設置-> Safari瀏覽器 -> 請求桌面網站開啓了,只要把它關掉就正常了,但是你又不能要求審覈人員或者用戶自己去把它關了,所以,在項目中手動關掉。
原因:使用移動設備獲取網站的視頻地址
if (@available(iOS 13.0, *)) {
    _appConfiguration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;
}

參考文章:WKWebView 在 iOS13 iPadOS 獲取到的UserAgent中的設備變成Macintosh的問題

未完待續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章