iOS9 - 採用3D Touch

iPhone 6s/6s Plus提供了觸摸屏的另外一個維度的操做手勢-3D Touch,一般有下面兩種應用場景:html

  1. 在主屏幕上重按APP圖標能夠提供進入APP特定功能的快捷菜單
  2. 在APP內部,能夠經過重按屏幕得到額外的快捷操做

 

主屏幕快捷菜單ios

 iOS 9 SDK提供了API來定義兩種類型的快捷菜單:web

  1. 靜態快捷菜單:在Info.plist定義UIApplicationShortcutItems數組
  2. 動態快捷菜單:使用UIApplicationShortcutItem類來定義菜單,使用UIApplication裏面的shortcutItems屬性來設定動態菜單

快捷菜單能夠顯示最多兩行文字和一個可選的圖標。api

 

一瞥&彈出 (Peek&Pop)數組

UIViewController能夠對用戶點擊有不一樣的響應,隨着按擊的深度不斷加深,頁面會有三個階段:app

  1. 標示內容預覽是能夠的ide

  2. 顯示預覽 (一瞥),同時提供快捷動做按鈕ui

  3. 選擇性的跳轉到新的頁面顯示預覽 (彈出)this

 

   

3D Touch APIspa

iOS 9提供以下3D Touch的API:

  1. 主屏幕快捷菜單API (Home screen quick action API)
  2. UIKit的一瞥和彈出API (UIKit peek and pop API)
  3. WebView的一瞥和彈出API (Web view peek and pop API)
  4. UITouch force屬性:讓你能夠自定義基於力度的用戶操做

無論使用何種API,你都須要在運行時來檢測3D Touch特性是否可用。

 

 

檢查3D Touch特性的可用性

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {             
  NSLog(@"你的手機支持3D Touch!");     
} else {         
  NSLog(@"你的手機暫不支持3D Touch!");     
}

 

主屏幕快捷菜單

1. 靜態菜單

能夠在Info.plist裏面定義靜態的菜單

2. 動態菜單

在AppDelegate裏使用UIApplicationShortcutItem來定義菜單,而後設置UIApplication的shortcutItems屬性

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"Open1" localizedTitle:@"Open1"];
    UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"Open2" localizedTitle:@"Open2"];
    NSArray *shortItems = [[NSArray alloc] initWithObjects:shortItem1, shortItem2, nil];
    [[UIApplication sharedApplication] setShortcutItems:shortItems];
    return YES;
}

處理菜單事件

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    if ([shortcutItem.localizedTitle isEqual: @"Open1"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OPPS!" message:@"Open1" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
}

 更多細節能夠參考

 

UIKit的一瞥和彈出(Peek&Pop)

Peek: 當用戶點擊某些特定的View能夠提供一些額外的內容

Pop: 跳轉到那個顯示額外內容的頁面

iOS 9 SDK包含如下3D Touch相關的特性

1. UIViewController裏面的新方法用來註冊或者註銷一個View Controller是否擁有3D Touch特性

2. ViewController的3D Touch的協議(Protocols)

 

支持Peek的快捷按鈕,iOS 9 SDK包含:

一般狀況下須要兼容是否支持3D Touch的設備,若是不支持3D Touch可使用UILongPressGesture來替代。

Interface ViewController () <UIViewControllerPreviewingDelegate>
{
    UILongPressGestureRecognizer *_longPress;
}
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)];
    _longPress = longPressGr;
}


//檢測頁面是否處於3DTouch
- (void)check3DTouch{
   
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
       
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
        NSLog(@'3D Touch 開啓');
        //長按中止
        _longPress.enabled = NO;
       
    }else{
        _longPress.enabled = YES;
    }
   
}


- (void)viewWillAppear:(BOOL)animated{
   
    [self check3DTouch];
   
}

實現UIViewController裏面關於3D Touch的Delegate方法

#pragma mark >> 3D touch 代理方法
//輕按進入浮動預覽頁面
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    ReviewViewController *vc = [ReviewViewController alloc] init];
    vc.view.frame = self.view.frame;
    UIImageView *er = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@'123.png']];
    vc.view = er;
    return vc;
}

添加預覽頁面的底部快捷菜單

//預覽頁面 底部Action Items
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    
    UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@'分享' style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@'點擊了分享');
    }];
    
    UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@'收藏' style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@'點擊了收藏');
    }];
    
    NSArray *actions = @[p1,p2];
    return actions;
}

 

WebView的一瞥和彈出

在WKWebView和UIWebView能夠設置屬性allowsLinkPreview來支持WebView的Peek和Pop。

1. 這個屬性是默認設置爲NO的

2. 若是使用WKWebView或者UIWebView,Pop會跳轉到Safari

3. 若是但願在APP內部保證Pop,建議使用SFSafariViewController來替代WKWebView

 

In iOS this property is available on devices that support 3D Touch. Default value is NO in iOS.

If you set this property’s value to YES, an iOS user can press links to preview link destinations and detected data such as addresses and phone numbers. Such previews are known to users as peeks. If a user presses deeper on a link preview, the preview navigates (or pops, in user terminology) to the destination. Because pop navigation switches the user from your app to Safari, it is opt-in for iOS apps.

If you want to support link preview in iOS but also want to keep users within your app, you can switch from using the WKWebView class to the SFSafariViewController class. If you are using a web view as an in-app browser, making this change is best practice. The Safari view controller class automatically supports link previews.

 

UITouch的force屬性

UITouch新增兩個屬性:

force

1. float值,1.0表明一般的按壓力度

2. 只在支持3D Touch的設備才生效,須要在使用前在運行時檢查3D Touch是否可用

The force of the touch, where a value of 1.0 represents the force of an average touch (predetermined by the system, not user-specific). (read-only)

This property is available on devices that support 3D Touch. To check at runtime if a device supports 3D Touch, read the value of the forceTouchCapability property on the trait collection for any object in your app with a trait environment.

 

maximumPossibleForce

用於設置按壓力度的最大值

 

總結

3D Touch爲iOS提供了一種在z軸方向的操做方式,更加豐富了屏幕操做的維度。在主屏幕和APP內部均可以提供內容預覽或者快捷菜單的操做,同時提供force屬性來檢測按壓力度,相信基於這個屬性能夠作出不少有趣的事情。

相關文章
相關標籤/搜索