iOS開發之3D Touch

一、簡介html

  3DTouch是在6s以後蘋果的一項技術,只能在6s及其以上機型真機運行,Xcode的模擬器是不支持的。app

  • Quick Actions(點擊icon的快捷方式)ide

  • Peek&Pop(應用內快速預覽內容)ui

  • UITouch和LivePhoto(讀取壓力和最大壓力)atom

 

二、Quick Actions(點擊icon的快捷方式)spa

  點擊app的圖標,能夠彈出Today小組件(若是有的話)和UIApplicationShortcutItem快捷方式——這就是Quick Actions!3d

//
//  UIApplicationShortcutItem.h
//  UIKit
//
//  Copyright © 2015-2017 Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class UIImage;

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,       //用戶自定義
    UIApplicationShortcutIconTypePlay,          //播放
    UIApplicationShortcutIconTypePause,         //暫停
    UIApplicationShortcutIconTypeAdd,           //添加
    UIApplicationShortcutIconTypeLocation,      //位置
    UIApplicationShortcutIconTypeSearch,        //搜索
    UIApplicationShortcutIconTypeShare,         //分享
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1), //禁止
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1), //聯繫人
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1), //家庭
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1), //標記位置
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1), //偏心
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1), //喜好
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1), //雲訪問
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1), //邀請
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1), //確認
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1), //郵箱
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1), //消息
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1), //日曆
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1), //時間
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1), //照片
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1), //視頻
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1), //建立任務
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1), //完成任務
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1), //鬧鐘
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1), //書籤
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1), //洗牌
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1), //音頻
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)  //更新
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;

NS_CLASS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED
@interface UIApplicationShortcutIcon : NSObject <NSCopying>
//初始化
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
//基於應用程序包中的圖像建立主屏幕快速動做圖標,最好是在資產目錄中。
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;

@end

NS_CLASS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED
@interface UIApplicationShortcutItem : NSObject <NSCopying, NSMutableCopying>

//初始化
- (instancetype)init NS_UNAVAILABLE;
/**
 @param type 自定義的字符串type
 @param localizedTitle 標題
 @param localizedSubtitle 子標題
 @param icon 圖標
 @param userInfo 傳遞的信息
 */
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle;

//讀取相關初始化信息
@property (nonatomic, copy, readonly) NSString *type;
@property (nonatomic, copy, readonly) NSString *localizedTitle;
@property (nullable, nonatomic, copy, readonly) NSString *localizedSubtitle;
@property (nullable, nonatomic, copy, readonly) UIApplicationShortcutIcon *icon;
@property (nullable, nonatomic, copy, readonly) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;

@end

NS_CLASS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED
//可變item
@interface UIMutableApplicationShortcutItem : UIApplicationShortcutItem
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *localizedTitle;
@property (nullable, nonatomic, copy) NSString *localizedSubtitle;
@property (nullable, nonatomic, copy) UIApplicationShortcutIcon *icon;
@property (nullable, nonatomic, copy) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;

@end

NS_ASSUME_NONNULL_END
UIApplicationShortcutItem相關API

  2.一、靜態建立代理

    直接配置Info.plist文件code

  2.二、動態建立  orm

    代碼建立:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSMutableArray *arrShortcutItem = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;
    
    UIApplicationShortcutItem *shoreItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"適得府君書咖啡來看" localizedTitle:@"搜索" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch] userInfo:nil];
    [arrShortcutItem addObject:shoreItem1];
    UIApplicationShortcutItem *shoreItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"杜師傅就快兩個打開分割" localizedTitle:@"新消息" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose] userInfo:nil];
    [arrShortcutItem addObject:shoreItem2];
    UIApplicationShortcutItem *shoreItem3 = [[UIApplicationShortcutItem alloc] initWithType:@"sdfasjdfhsksdjfsdajklfdjfh" localizedTitle:@"加大開發" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation] userInfo:nil];
    [arrShortcutItem addObject:shoreItem3];
    UIApplicationShortcutItem *shoreItem4 = [[UIApplicationShortcutItem alloc] initWithType:@"sdfasjdfhskdjfh" localizedTitle:@"" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare] userInfo:nil];
    [arrShortcutItem addObject:shoreItem4];

    [UIApplication sharedApplication].shortcutItems = arrShortcutItem;
    return YES;
}

  2.三、點擊響應方法

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    NSLog(@"name ==%@\n type =  %@",shortcutItem.localizedTitle,shortcutItem.type);
}

  ps:系統優先加載靜態方法建立的item,能夠混合加載,最多4個item;

    正式發佈後會有5個item,蘋果統一添加個分享的item;

    有的item的圖標會顯如今右側,這個和app在屏幕得位置有關;

 

 三、Peek&Pop(應用內快速預覽內容)

  Peek頁面預覽:給視圖添加一點壓力,會以彈框的方式預覽目標界面;

  Pop拉出菜單:在界面預覽時,若是有菜單選項,向上拖拽下面會出現菜單欄;

  繼續增長壓力會跳轉到目標界面——也就是Peek&Pop!

  3.一、Peek

    本例在圖片上添加此功能:前提一個是能夠交互、二是註冊registerForPreviewingWithDelegate

    [self.imageView setUserInteractionEnabled:YES];
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:(id)self sourceView:self.imageView];
    }

    建立菜單欄:須要在目標ViewController界面重寫- (NSArray<id<UIPreviewActionItem>> *)previewActionItems;方法

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action2");
    }];
    NSArray *actions = @[action1,action2];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions];

    return@[action1,action2,group1];
}

    預覽視圖方法和跳轉視圖方法:

//預覽
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)
{
    if ([self.presentedViewController isKindOfClass:[VideoViewController class]]){
        return nil;
    }else {
        VideoViewController *contentVC = [[VideoViewController alloc] init];
        return contentVC;
    }
}

//重按進入
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)
{
    NSLog(@"重按進入");
    VideoViewController *v = [[VideoViewController alloc] init];
    [self presentViewController:v animated:YES completion:nil];
}

 

四、UITouch和LivePhoto(讀取壓力和最大壓力)

  在UITouch類中有兩個方法用來讀取實時壓力和最大壓力:

// 觸摸壓力值
@property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0);
// 最大觸摸壓力值
@property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0);

  在本例中重寫了ImageView的代理方法獲取這兩個數值:

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"=======%lf",touches.anyObject.force);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"------%lf",touches.anyObject.maximumPossibleForce);
}

  結果:

  LivePhoto是一張圖片和一段3秒的視頻組成,當使用3D Touch時會播放這段視頻,手指放開會結束播放。

  因此這個效果經過UITouch的上面兩個屬性就能夠本身完成!

相關文章
相關標籤/搜索