3DTouch相關

demo下載      app

一、Home Screen Quick Actionside

效果以下:ui

 

 

實現方法一:(用代碼寫)spa

在AppDelegate.m  的  application: didFinishLaunchingWithOptions:方法中建立快捷item,設計

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2     // Override point for customization after application launch.
 3     
 4     // 建立只有標題的item
 5     UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"type1" localizedTitle:@"標題1"];
 6     // 建立有標題,且有副標題的item
 7     UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"type2" localizedTitle:@"標題2" localizedSubtitle:@"副標題" icon:nil userInfo:nil];
 8     
 9     // 獲取icon對象(系統樣式icon)
10     UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome];
11     // 建立有標題,有副標題,有圖片(系統)的item
12     UIApplicationShortcutItem *item3 = [[UIApplicationShortcutItem alloc] initWithType:@"type3" localizedTitle:@"標題3" localizedSubtitle:@"系統圖標" icon:icon userInfo:nil];
13     
14     UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"feiji"];
15     // 有標題,有副標題,有圖片(自定義)的item
16     UIApplicationShortcutItem *item4 = [[UIApplicationShortcutItem alloc] initWithType:@"type4" localizedTitle:@"標題4" localizedSubtitle:@"自定義icon" icon:icon2 userInfo:nil];
19 // 添加到icon快捷方式 20 [UIApplication sharedApplication].shortcutItems = @[item1,item2,item3,item4]; 24 return YES; 25 }

實現方法二(修改info.plist)3d

 

 1 <key>UIApplicationShortcutItems</key>
 2     <array>
 3         <dict>
 4             <key>UIApplicationShortcutItemType</key>
 5             <string>title_one</string>
 6             <key>UIApplicationShortcutItemTitle</key>
 7             <string>標題1</string>
 8             <key>UIApplicationShortcutItemSubtitle</key>
 9             <string>副標題1</string>
10         </dict>
11         <dict>
12             <key>UIApplicationShortcutItemType</key>
13             <string>title_one</string>
14             <key>UIApplicationShortcutItemTitle</key>
15             <string>標題2</string>
16             <key>UIApplicationShortcutItemSubtitle</key>
17             <string>副標題2</string>
18             <key>UIApplicationShortcutItemIconType</key>
19             <string>UIApplicationShortcutIconTypeLove</string>
20         </dict>
21         <dict>
22             <key>UIApplicationShortcutItemType</key>
23             <string>title_one</string>
24             <key>UIApplicationShortcutItemTitle</key>
25             <string>標題3</string>
26             <key>UIApplicationShortcutItemSubtitle</key>
27             <string>副標題3</string>
28             <key>UIApplicationShortcutItemIconType</key>
29             <string>UIApplicationShortcutIconTypeLove</string>
30             <key>UIApplicationShortcutItemIconFile</key>
31             <string>feiji</string>
32         </dict>
33     </array>

 

點擊某一個item時會調用code

application:performActionForShortcutItem:completionHandler:orm

在在AppDelegate.m中,重寫此方法。對象

 1 // 點擊某一個item時調用
 2 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
 3 
 4     // 根據shortcutItem.type 判斷點擊了哪個item
 5     if ([shortcutItem.type isEqualToString:@"type1"]) {
 6         // 作相應 操做 8     }else if ([shortcutItem.type isEqualToString:@"type2"]){10     }
11 }

 

二、peek and popblog

這個功能是一套全新的用戶交互機制,在使用3D Touch時,ViewController中會有以下三個交互階段:

(1)提示用戶這裏有3D Touch的交互,會使交互控件周圍模糊

(2)繼續深按,會出現預覽視圖

(3)經過視圖上的交互控件進行進一步交互

        這個模塊的設計能夠在網址鏈接上進行網頁的預覽交互。

實現協議 UIViewControllerPreviewingDelegate

實現方法

 1 #pragma mark - UIViewControllerPreviewingDelegate
 2 
 3 // peek
 4 - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
 5     
 6  
 7     // 轉換座標
 8     location = [self.tableView convertPoint:location fromView:[previewingContext sourceView]];
 9     
10     // 根據location獲取位置
11     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
12     
13     // 建立控制器
14     ViewController *vc = [[ViewController alloc] init];
15     
16     return vc;
17     
18 }
19 
20 // pop
21 - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
22     
23     // push peek 所看到的UIViewController
24     [self.navigationController pushViewController:viewControllerToCommit animated:YES];
25     
26 }

注意,使用tableView實現peek和pop,須要在返回cell前這樣寫(調用registerForPreviewingWithDelegate:sourceView:方法

// 告訴系統每個cell均可以被peek

[self registerForPreviewingWithDelegate:self sourceView:cell];

 

這個效果的實現,在peek返回的 UIViewController 中重寫 -previewActionItems 方法

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    
    // 建立贊操做
    UIPreviewAction *item1 = [UIPreviewAction actionWithTitle:@"" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"讚了%@",self.httpUrl);
    }];
    
    UIPreviewAction *item2 = [UIPreviewAction actionWithTitle:@"評論" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"評論了%@",self.httpUrl);
    }];
    
    UIPreviewAction *item3 = [UIPreviewAction actionWithTitle:@"item3" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"item3");
    }];
    
    // 建立一個組包涵贊和評論
    UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"" style:UIPreviewActionStyleDefault actions:@[item1,item2]];
    
    return @[group,item3];
    
}

 

三、Force Properties

iOS9爲咱們提供了一個新的交互參數:力度。咱們能夠檢測某一交互的力度值,來作相應的交互處理。例如,咱們能夠經過力度來控制快進的快慢,音量增長的快慢等。

當用力按壓屏幕時會調用(即便,手指不滑動)   -touchesMoved:withEvent: 方法

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    獲取觸摸對象
    UITouch *t = touches.anyObject;
    t.force // 返回按壓力度值(float),實測最大值爲6.6666666,最小應該是0.
}
相關文章
相關標籤/搜索