標籤:led ref fhs register shuffle 個數 confirm ali 混合
1、簡介
3DTouch是在6s之後蘋果的一項技術,只能在6s及其以上機型真機運行,Xcode的模擬器是不支援的。
2、Quick Actions(點擊icon的捷徑)
點擊app的表徵圖,可以彈出Today小工具(如果有的話)和UIApplicationShortcutItem捷徑——這就是Quick Actions!
//// 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;@endNS_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;@endNS_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;@endNS_ASSUME_NONNULL_END
UIApplicationShortcutItem相關API
2.1、靜態建立
直接配置Info.plist檔案
2.2、動態建立
代碼建立:
- (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.3、點擊回應程式法
- (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在螢幕得位置有關;
3、Peek&Pop(應用內快速預覽內容)
Peek頁面預覽:給視圖添加一點壓力,會以彈框的方式預覽目標介面;
Pop拉出菜單:在介面預覽時,如果有菜單選項,向上拖拽下面會出現功能表列;
繼續增加壓力會跳轉到目標介面——也就是Peek&Pop!
3.1、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];}
4、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的上面兩個屬性就可以自己完成!
iOS開發之3D Touch