IOS9 provides four classes of APIs ( Home Screen Quick Action , UIKit Peek & Pop , WebView Peek & Pop and UITouch Force Properties ) for manipulating 3D Touch. However, whichever API you use, the first thing you need to do is check if 3D touch is available.
Detects if 3D Touch is supported
The following interfaces are provided in iOS9 to check if the device supports 3D Touch:
@property(nonatomic, readonly) UIForceTouchCapability forceTouchCapability;
This UIForceTouchCapability is an enumeration type, which is described in the following scenario:
- Uiforcetouchcapability
- Uiforcetouchcapabilityunknown//3d Touch Detection failed
- Uiforcetouchcapabilityunavailable//3d Touch is not available
- Uiforcetouchcapabilityavailable//3d Touch Available
These 3 enumeration values let us determine whether the device turns on the 3D touch function, and can be judged in the viewwillappear of the Uiviewcontroller lifecycle:
if (self.traitCollection.forceTouchCapability = = uiforcetouchcapabilityavailable) {//do Something}
Of course, in the life cycle, if the user intentionally modifies the device's 3D touch functionality, we have a place to re-detect:
-(void) Traitcollectiondidchange: (uitraitcollection *) previoustraitcollection { // Do Something}
Home Screen Quick Action
There are two ways to create a quick action: static and dynamic
① create in a static manner
Statically created is declared in the Info.plist file.
<key>UIApplicationShortcutItems</key><array> <dict> <key> Uiapplicationshortcutitemicontype</key> <string>uiapplicationshortcuticontypeshare</string> <key>UIApplicationShortcutItemTitle</key> <string> Share </string> <key>UIApplicationShortcutItemType</key> <string>uitouchtext.share</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>key1</ke Y> <string>value1</string> </dict> </dict> <dict> <key>UIApplicationShortcutItemType</key> &L Tstring>com.devzeng.about</string> <key>UIApplicationShortcutItemTitle</key> <string> About Us </string> <key>UIApplicationShortcutItemSubtitle</key> <string> This is about us </string> <key>UIApplicationShortcutItemIconFile</key> <string>icon_about.png</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>scheme</key> ; <string>devzeng://about</string></dict> </dict></array>
② to create in a dynamic manner
Dynamic creation is dynamically added with code when the program is initialized. The UIApplication object has an array of supported shortcuts (SHORTCUTITEMS) and can be assigned to the Shortcutitems property if you need to add a shortcut.
In the Method-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
Uiapplicationshortcuticon *icon = [Uiapplicationshortcuticon iconwithtemplateimagename:@"1234"]; Uiapplicationshortcutitem*item = [[Uiapplicationshortcutitem alloc] Initwithtype:@"search22"Localizedtitle:@"search22"Localizedsubtitle:@"0ABC"Icon:icon Userinfo:nil]; Application.shortcutitems= @[item];
Description
1) The system restricts the ability for each app to display up to 4 action Item, including static and dynamic mode;
2) If the static and dynamic modes are used simultaneously, the shortcutitems assigned to UIApplication will not be overwritten
3) Note that the icon defined is a monochrome icon for 35x35
UIApplication provides a callback method when the app is in the background, as follows
- (void) Application: (UIApplication *) application Performactionforshortcutitem: (Uiapplicationshortcutitem *) Shortcutitem Completionhandler: (void(^) (BOOL succeeded)) completionhandler{//determine the unique identity we set previously if([Shortcutitem.type isequaltostring:@"Uitouchtext.share"]) {Nsarray*arr = @[@"Hello 3D Touch"]; Uiactivityviewcontroller*VC =[[Uiactivityviewcontroller Alloc]initwithactivityitems:arr Applicationactivities:nil]; //set the current VC to ROOTVC[Self.window.rootViewController PRESENTVIEWCONTROLLER:VC Animated:yes completion:^{ }]; } Else if([Shortcutitem.type isequaltostring:@"Uitouchtext.search"]) {Uialertview*alertview = [[Uialertview alloc]initwithtitle:@"Warm Tips"Message@"I miss you." Delegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"sure", nil]; [Alertview show]; } Else if([Shortcutitem.type isequaltostring:@"Uitouchtext.look"]) {Uiactionsheet*sheet = [[Uiactionsheet alloc]initwithtitle:@"Warm Tips" Delegate: Nil Cancelbuttontitle:@"Cancel"Destructivebuttontitle:@"Delete"Otherbuttontitles:@"more", nil]; [Sheet ShowInView:self.window]; } Else if([Shortcutitem.type isequaltostring:@"Uitouchtext.compose"]) {NSLog (@"Uitouchtext.compose"); }}
We do different event handling based on the type and userinfo of the Shortcutitem in this callback, and the final Completionhandler in the API's description we see that when the application is not in the background, but directly re-open the process, directly return no, So this time, our callbacks will be placed in
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
UIApplication also gave us a key to get this shortcutitem from Launchoptions (Uiapplicationlaunchoptionsshortcutitemkey)
Uiapplicationshortcutitem *item = [launchoptions Valueforkey:uiapplicationlaunchoptionsshortcutitemkey]; // respond to different events depending on the action
Peek and Pop (preview and read more)
Like zooming in with a magnifying glass, previewing (Peek) content (PHOTOS, videos) allows you to get more information without having to load the full content.
Instagram first adds peek functionality to small images and videos.
Peek (preview) and pop (read) related API is Uiviewcontrollerpreviewingdelegate, which is registered through view. On Instagram, we only register it on the view of the controller that can accept the touch event. When a 3D touch event occurs, delegate detects which object in the view is clicked.
If delegate determines that a Peek (preview) action occurs, it still needs to handle two things: 1. Sets the rect;2 of the view that the preview action takes place. Returns the displayed Viewcontroller.
When 3D touch occurs, it is passed to you with the context information that contains the source view and the point where the click occurred. The custom delegate needs to be mapped to a view through this point, and then find the data that needs to be previewed.
Most Instagram features are based on Uicollectionview and UITableView implementations. Both have good API support for data and UI correspondence.
-(Uiviewcontroller *) Previewingcontext: (ID) Previewingcontext viewcontrollerforlocation: (cgpoint) Location {Uicollectionview*collectionview = (Uicollectionview *) [Previewingcontext Sourceview]; Nsindexpath*indexpath =[CollectionView indexpathforitematpoint:location]; if(Indexpath) {Igpostcell*cell =[CollectionView Cellforitematindexpath:indexpath]; [Previewingcontext SetSourceRect:cell.frame]; Igpost*post =Self.feedcontroller.posts[indexpath.row]; Igthumbnailpreviewcontroller*controller =[[Igthumbnailpreviewcontroller alloc] initwithpost:post]; returnController; } //No Peek returnNil;}
When you combine preview-related APIs with existing views and features, viewing Instagram photos and videos becomes surprisingly simple
We have added the ability to view information for the username in the header and comment. The delegate used to preview the user's information is similar to the implementation of the photo/video display:
Locate the corresponding cell using the location of the 3D touch click and the index path found.
Converts a clicked Point Association to a textview in the cell.
This point gets the properties of the attributed string into the text view.
If a property of the user name is found, the Iguserpreviewcontroller is returned.
String these implementations together and look like this:
Mixed content Display
The activity Feed feature in Instagram includes the user's tag and thumbnail. We have previously created Uiviewcontrollerpreviewingdelegate objects that are used to display both types of content. Here we need to combine these together.
It is unwise to copy the existing code into the new delegate. We chose to use the composition mode to assemble the delegate code for both the profile and the post display, creating a new object that only needs to be passed the touch event.
@interfaceIguserthumbnailpreviewinghandler:nsobject@property (nonatomic, strong) Iguserpreviewinghandler*userdelegate, @property (nonatomic, strong) Igfeedthumbnailpreviewinghandler*thumbnaildelegate;@end-(Uiviewcontroller *) Previewingcontext: (iduiviewcontrollerpreviewing>) Previewingcontext viewcontrollerforlocation: (cgpoint) Location {IDController =[Self.thumbnaildelegate Previewingcontext:previewingcontext viewcontrollerforlocation:location]; if(!Controller) {Controller=[Self.userdelegate Previewingcontext:previewingcontext viewcontrollerforlocation:location]; } returnController;}
The controller used to display the preview is only a subclass of Uiviewcontroller, which differs in some special attributes.
At first, the controller was completely unable to interact, and you couldn't click on the button or add a custom gesture. Here we need to provide an array of objects that follow the Uipreviewactionitem protocol. In Instagram, we only use uipreviewactions.
These features look much like Uialertcontroller's action item.
Throughout the Instagram program, we only load the data when we need it and cache it for later use. This saves the time that the network waits and does not waste the user's bandwidth.
Sometimes, when previewing a user profile, we don't get enough data to show the latest photos or the number of followers. The preview view controller will still call Viewdidload, Viewwillappear: and other Uiviewcontroller events. Where you can get network data and update the UI.
Finally, we find that we can adjust the size of the preview view by modifying the controller's preferredcontentsize, which can still be applied even if the controller has already shown it. However, this property cannot be used in animations.
Reference: Http://git.devzeng.com/blog/ios9-3d-touch.html?utm_source=tuicool&utm_medium=referral
Http://engineering.instagram.com/posts/465414923641286/lessons-learned-with-3D-touch
Http://www.cocoachina.com/ios/20151110/14129.html
Using 3D Touch in iOS9