iphone 6s給人驚喜之一就是3D Touch,今天就來瞅瞅它.
UITouch有個force屬性,一般的壓力感應值為1.0,可以設置他的maximumPossibleForce最大值,即從0到這個值的范圍.可以利用這個壓感值來畫出不同粗細的線條.
if traintCollection.forceTouchCapability == .Available {
addLineFromPoint(touch.previousLocationInView(self),
toPoint: touch.locationInView(self), withForce: touch.force)
}
CGContextSetLineWidth(cxt, force * strokeWidth)
效果如下:
Screen Shot 2015-10-26 at 8.45.34 PM.png
- Peek Pop
當你點擊6s上的郵件列表中的其中一個郵件時,其他郵件會變為半透明,以突出你現在點擊的郵件,而當你繼續按壓時,則會彈出這個郵件的內容,這個即為Peek(下圖2),繼續按壓,郵件內容會顯示為全屏模式,即為Pop(下圖4).
Screen Shot 2015-10-26 at 8.51.43 PM.png
如果要實現Peek和Pop需要實現代理方法
if traitCollection.forceTouchCapability == .Available {
registerForPreviewingWithDelegate(self, sourceView: view)
}
extension ViewController: UIViewControllerPreviewingDelegate {
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let indexPath = tableView.indexPathForRowAtPoint(location),
cell = tableView.cellForRowAtIndexPath(indexPath) as? DoodleCell
else { return nil }
let identifier = "DoodleDetailViewController"
guard let detailVC = storyboard?.instantiateViewControllerWithIdentifier(identifier) as? DoodleDetailViewController
else { return nil }
detailVC.doodle = cell.doodle
detailVC.doodlesViewController = self
previewingContext.sourceRect = cell.frame
// peek
return detailVC
}
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
//pop!
showViewController(viewControllerToCommit, sender: self)
}
}
detailVC是Peek的內容.
- Preview actions
可以為Peek內容增加上提操作:
override func previewActionItems() -> [UIPreviewActionItem] {
let shareAction = UIPreviewAction(title: "Share", style: .Default) {
(previewAction, viewController) in
if let doodlesVC = self.doodlesViewController,
activityViewController = self.activityViewController {
doodlesVC.presentViewController(activityViewController, animated:true, completion: nil)
}
}
let deleteAction = UIPreviewAction(title: "Delete",
style: .Destructive) {
(previewAction, viewController) in
guard let doodle = self.doodle else { return }
Doodle.deleteDoodle(doodle)
if let doodlesViewController = self.doodlesViewController {
doodlesViewController.tableView.reloadData()
}
}
return [shareAction, deleteAction]
}
效果如下:
Screen Shot 2015-10-26 at 9.29.08 PM.png
<h3>Home screen quick actions</h3>
Screen Shot 2015-10-26 at 9.32.42 PM.png
有兩種添加方法:
- 靜態添加
直接在plist文件里添加.
Screen Shot 2015-10-26 at 9.34.55 PM.png
在AppDelegate文件里添加:
func application(application: UIApplication,
performActionForShortcutItem shortcutItem: UIApplicationShortcutItem,
completionHandler: (Bool) -> Void) {
handleShortcutItem(shortcutItem)
completionHandler(true)
}
func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {
switch shortcutItem.type {
case "com.razeware.Doodles.new":
presentNewDoodleViewController()
default: break
}
}
func presentNewDoodleViewController() {
let identifier = "NewDoodleNavigationController"
let doodleViewController = UIStoryboard.mainStoryboard
.instantiateViewControllerWithIdentifier(identifier)
window?.rootViewController?
.presentViewController(doodleViewController, animated: true,
completion: nil)
}
- 動態添加
static func configureDynamicShortcuts() {
if let mostRecentDoodle = Doodle.sortedDoodles.first {
let shortcutType = "com.razeware.Doodles.share"
let shortcutItem = UIApplicationShortcutItem(type: shortcutType, localizedTitle: "Share Latest Doodle",
localizedSubtitle: mostRecentDoodle.name,
icon: UIApplicationShortcutIcon(type: .Share),
userInfo: nil)
UIApplication.sharedApplication().shortcutItems = [ shortcutItem ]
} else {
UIApplication.sharedApplication().shortcutItems = []
}
拓展: