Last active
December 21, 2016 07:13
-
-
Save shoheiyokoyama/5efb9dd4a08679d6a5fa6f14ffde2a96 to your computer and use it in GitHub Desktop.
Widgets(Today Extension)のまとめ ref: http://qiita.com/shoheiyokoyama/items/16593eb170860acd7344
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum NCUpdateResult : UInt { | |
//最新の情報を表示したい場合 | |
case newData | |
//最新状態から変更がなく、更新する必要がない場合 | |
case noData | |
//アップデートに失敗した際 | |
case failed | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func tappedButton(_ sender: Any) { | |
let url = URL(string: "todaySample://") | |
extensionContext?.open(url!, completionHandler: nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NotificationCenter | |
let widgetController = NCWidgetController() | |
widgetController.setHasContent(flg, forWidgetWithBundleIdentifier: "com.Today.TodayExtension") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@available(iOS 10.0, *) | |
public enum NCWidgetDisplayMode : Int { | |
case compact // Fixed height | |
case expanded // Variable height | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// expandedで拡大可能に | |
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded | |
// NCWidgetDisplayModeが変更されるたびに呼ばれるので、ここで拡大時の高さを設定する。 | |
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) { | |
if case .compact = activeDisplayMode { | |
preferredContentSize = maxSize | |
} else { | |
preferredContentSize.height = 250 | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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