Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active December 21, 2016 07:13
Show Gist options
  • Save shoheiyokoyama/5efb9dd4a08679d6a5fa6f14ffde2a96 to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/5efb9dd4a08679d6a5fa6f14ffde2a96 to your computer and use it in GitHub Desktop.
Widgets(Today Extension)のまとめ ref: http://qiita.com/shoheiyokoyama/items/16593eb170860acd7344
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { [weak self] context in
self?.textLabel.alpha = 0.3
self?.imageView.alpha = 0.3
}, completion: { [weak self] _ in
self?.textLabel.alpha = 1
self?.imageView.alpha = 1
})
}
public enum NCUpdateResult : UInt {
//最新の情報を表示したい場合
case newData
//最新状態から変更がなく、更新する必要がない場合
case noData
//アップデートに失敗した際
case failed
}
@IBAction func tappedButton(_ sender: Any) {
let url = URL(string: "todaySample://")
extensionContext?.open(url!, completionHandler: nil)
}
import NotificationCenter
let widgetController = NCWidgetController()
widgetController.setHasContent(flg, forWidgetWithBundleIdentifier: "com.Today.TodayExtension")
@available(iOS 10.0, *)
public enum NCWidgetDisplayMode : Int {
case compact // Fixed height
case expanded // Variable height
}
// expandedで拡大可能に
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
// NCWidgetDisplayModeが変更されるたびに呼ばれるので、ここで拡大時の高さを設定する。
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if case .compact = activeDisplayMode {
preferredContentSize = maxSize
} else {
preferredContentSize.height = 250
}
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
fetchTodayData() { data, error in
if let _ = error {
completionHandler(.failed)
} else {
updateUI(data)
completionHandler(.newData)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment