iOS system 3DTouch full resolution
Overview
IOS10 system Login In China, the use of 3D touch is more frequent in the system, so it is necessary to do some understanding of the 3D touch function introduced in iOS9.
In daily development, we often need to use two features in 3D touch
Use 3DTouch action on the app icon on the home screen
Using 3DTouch operations on a control within an application
Shortcutitem Overview
The Shortcutitem feature allows the user to use 3DTouch on the app icon on the home screen, and if the operation is valid, a few quick options are given to allow the user to operate
Static add
Add the Uiapplicationshortcutitems keyword in info.plist to configure it as follows
The definitions of each of these keywords are as follows:
Uiapplicationshortcutitemtype: Specific string of shortcut options (required)
Uiapplicationshortcutitemtitle: Title of the shortcut option (required)
Uiapplicationshortcutitemsubtitle: Sub-title for shortcut options (optional)
Uiapplicationshortcutitemicontype: Icon for shortcut options (optional)
Uiapplicationshortcutitemiconfile: Custom icon for shortcut options (optional)
Uiapplicationshortcutitemuserinfo: Additional Information for shortcut options (optional)
Add Uiapplicationshortcutitem Dynamically
Each shortcut option is a Uiapplicationshortcutitem object with the specified initializer (Ns_designated_initializer) as follows
-(Instancetype) Initwithtype: (NSString *) type localizedtitle: (NSString *) Localizedtitle Localizedsubtitle: (Nullable NSString *) Localizedsubtitle icon: (Nullable Uiapplicationshortcuticon *) icon UserInfo: (Nullable nsdictionary *) UserInfo;
The explanations of each parameter are as follows:
Type: A specific string of shortcut options (required)
Localizedtitle: Title of the shortcut option (required)
Localizedsubtitle: Sub-title for shortcut options (optional)
Icon: Icons for shortcut options (optional)
UserInfo: Additional Information for shortcut options (optional)
Uiapplicationshortcuticon
Each shortcut icon is a Uiapplicationshortcuticon object, we can use multiple system-provided icons, or we can customize our own icons
Use system-provided icons + (Instancetype) Iconwithtype: (uiapplicationshortcuticontype) type;//custom icon + (instancetype) Iconwithtemplateimagename: (NSString *) templateimagename;
The system provides the following icon styles:
Note: The custom icon needs to use the skeleton icon, and 1 time times the icon size is recommended 35*35
Example
1234567891011121314 |
// 以"动态添加四个快捷可选项"为例
UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@
"com.liupeng.search"
localizedTitle:@
"Search"
localizedSubtitle:@
"Search Subtitle"
icon:searchShortcutIcon userInfo:nil];
UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@
"com.liupeng.play"
localizedTitle:@
"Play"
localizedSubtitle:@
"Play Subtitle"
icon:playShortcutIcon userInfo:nil];
UIApplicationShortcutIcon *qrShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@
"shortcut_QR"
];
UIApplicationShortcutItem *qrShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@
"com.liupeng.qr"
localizedTitle:@
"QR" localizedSubtitle:@
"QR Subtitle"
icon:qrShortcutIcon userInfo:nil];
UIApplicationShortcutIcon *payShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@
"shortcut_pay"
];
UIApplicationShortcutItem *payShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@
"com.liupeng.pay"
localizedTitle:@
"Pay"
localizedSubtitle:@
"Pay Subtitle"
icon:payShortcutIcon userInfo:nil];
[UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem, qrShortcutItem, payShortcutItem];
|
Trigger Callback
When the user enters the application by clicking on the shortcut option, the following method can be called back, which allows us to differentiate it by the type of the shortcut option for different operations
123456789101112 |
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
if ([shortcutItem.type isEqualToString:@
"com.liupeng.search"
])
{
// do something ...
}
else
if
([shortcutItem.type isEqualToString:@
"com.liupeng.play"
])
{
// do something ...
}
// ...
}
|
Attention
Before adding shortcut options dynamically, you need to determine if 3D touch is supported to avoid running programs on unsupported devices to cause a flash
1234567891011121314 |
if
([self respondsToSelector:@selector(traitCollection)])
{
if
([self.traitCollection respondsToSelector:@selector(forceTouchCapability)])
{
if
(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
// 支持3D Touch
}
else
{
// 不支持3D Touch
}
}
}
|
Note: On a device that supports 3D touch, users can turn off the 3D touch feature by setting up the universal-----accessibility---3D touch during program operation, so we need to rewrite the-traitcollectiondidchange: method to handle at any time
Peek & Pop Overview
Peek and Pop is a new interactive pattern within the app, and when the user is constantly adding strength to the controls, it moves into four stages
Use
The use of Peek & Pop is explained in the example "Uilabel control from Viewcontroller through Peek & Pop entry Detailviewcontroller"
12 |
@interface ViewController () <uiviewcontrollerpreviewingdelegate> @end</uiviewcontrollerpreviewingdelegate> |
1 |
[self registerForPreviewingWithDelegate:self sourceView:label]; |
123456789101112 |
- (nullable UIViewController *)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
// previewingContext.sourceView: 触发Peek & Pop操作的视图
// previewingContext.sourceRect: 设置触发操作的视图的不被虚化的区域
DetailViewController *detailVC = [[DetailViewController alloc] init];
// 预览区域大小(可不设置)
detailVC.preferredContentSize = CGSizeMake(0, 300);
return
detailVC;
}</uiviewcontrollerpreviewing>
|
1234 |
- (void)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { [self.navigationController pushViewController:viewControllerToCommit animated:YES]; }</uiviewcontrollerpreviewing> |
123456789101112131415161718 |
- (NSArray<id<uipreviewactionitem>> *)previewActionItems
{
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@
"选项一"
style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@
"选项二" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@
"选项三"
style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
}];
UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@
"选项组"
style:UIPreviewActionStyleDefault actions:@[action1, action2]];
return
@[action1, action2, action3, actionGroup];
}</id<uipreviewactionitem>
|
iOS system 3DTouch full resolution