Using the 3D Touch Peek and Pop functionality, what is the most effective way of mimicking the capability depicted below (to swipe the "peeked" content side-to-side to perform an action)? The screenshot below comes from the iOS native Mail app.
Update
You can replicate this effect on iOS 10. There's a new set of APIs called UIPreviewInteraction
and UIPreviewInteractionDelegate
that enable custom presentation for peek/pop interactions. I highly recommend watching A Peek at 3D Touch from this year's WWDC.
Original Answer
Looking at the iOS Runtime Headers, there's a class called UIPreviewPresentationController
. That's the controller responsible for peeks. Inside it, there are references to objects called leadingPreviewAction
and trailingPreviewAction
. These have corresponding properties that deal with edge constraints and centers. Judging by use of the terms leading and trailing (as in Auto Layout), these may correspond to left/right action items.
This is speculation, but I think these related private classes (_UIPreviewQuickActionView
) control the behavior you're looking for. Those are unavailable right now.
Likewise, the documentation for UIPreviewAction
says:
A preview action, or peek quick action, is displayed below a peek when a user swipes the peek upward. A peek quick action typically selects a deep link to your app and has a title, a style, and a handler.
To use Peek Quick Actions, your detailledViewControler (or whatever you named it) should override previewActionItems
like that:
lazy var previewActions: [UIPreviewActionItem] = {
func previewActionForTitle(title: String, style: UIPreviewActionStyle = .Default) -> UIPreviewAction {
return UIPreviewAction(title: title, style: style) { previewAction, viewController in
guard let detailViewController = viewController as? DetailViewController,
item = detailViewController.detailItemTitle else { return }
print("\(previewAction.title) triggered from `DetailViewController` for item: \(item)")
}
}
let action1 = previewActionForTitle("Default Action")
let action2 = previewActionForTitle("Destructive Action", style: .Destructive)
let subAction1 = previewActionForTitle("Sub Action 1")
let subAction2 = previewActionForTitle("Sub Action 2")
let groupedActions = UIPreviewActionGroup(title: "Sub Actions…", style: .Default, actions: [subAction1, subAction2] )
return [action1, action2, groupedActions]
}()
override func previewActionItems() -> [UIPreviewActionItem] {
return previewActions
}
You can find the whole code here : https://developer.apple.com/library/ios/samplecode/ViewControllerPreviews/ViewControllerPreviewsUsingtheUIViewControllerpreviewingAPIs.zip
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With