[英]iOS 9 Quick Actions (3D Touch)
我試圖了解針對iOS 9進行快速操作(3D觸摸)的方法。
我希望用戶選擇要應用於圖像的4個濾鏡中的1個,因此,如果我選擇第1個項目,則將NSUserDefaults.standardUserDefaults()設置為濾鏡,然后使用所應用的濾鏡顯示正確的圖片。
在AppDelete.swift中:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
var filterType:Int
switch (shortcutItem.type) {
...set filterType
}
NSUserDefaults.standardUserDefaults().setInteger(filterType, forKey:"filterType")
NSUserDefaults.standardUserDefaults().synchronize()
}
在ViewController.swift中:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"setDefaultFilter", name: UIApplicationWillEnterForegroundNotification, object:nil) // Handle enter from background
setDefaultFilter()
}
func setDefaultFilter() {
filterType = defaults.integerForKey("filterType")
...
imageView.image = filterImage(defaultImage!, filter:filters[filterType])
}
但是,從菜單進入應用程序時,它將始終顯示最后一個選擇(而不是當前選擇)。 如果我選擇項目1,則什么也沒發生。 我選擇項目3,項目1將出現。
我也嘗試通過appDelegate傳遞參數,結果是相同的。 我相信生命周期存在一些問題。
有任何想法嗎?
NSUserDefaults將數據寫入閃存,但速度可能不那么快。
您可以等待更長的時間,例如觀察UIApplicationDidBecomeActiveNotification
而不是UIApplicationWillEnterForegroundNotification
。
或者,您可以使用其他方式傳遞參數,例如,作為AppDelegate
的實例變量。
在調用performActionForShortcutItem方法以響應快速操作之前,始終會調用didFinishLaunchingWithOptions方法。 因此,我認為您需要檢查在didFinishLaunchingWithOptions方法中選擇了哪種快速操作。 如果未通過快速操作啟動應用程序,則只需繼續正常的應用程序啟動過程即可。(默認過濾器)
而且,如果您決定在didFinishLaunchingWithOptions中處理快速動作,則必須在didFinishLaunchingWithOptions中返回NO。
您可以從我的演示項目中獲得更多想法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.