Skip to content

Instantly share code, notes, and snippets.

import UIKit
import SwifterSwift
class ViewController: UIViewController {
override func viewDidLoad() {
// 日期處理
var date: Date = Date()
print("isInToday: \(date.isInToday)") // 判斷日期是否為今天
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainVC: ViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 設定 Controller 的入口
// MARK: - LifeCycle
// ---------------------------------------------------------------------
override func viewDidLoad() {
let w: CGFloat = self.view.frame.size.width
let h: CGFloat = self.view.frame.size.height - 20
// 產生UIScrollView
self.scrollView = UIScrollView()
self.scrollView!.frame = CGRect(x: 0, y: 0, width: w, height: h)
self.scrollView!.contentSize = CGSize(width: w * CGFloat(self.movieImage.count), height: h)
// 自動切頁
func onAutoChangePageAction() {
self.scrollView.setContentOffset(CGPoint(x: self.scrollView.frame.size.width * CGFloat(self.currenPageNumber), y: 0), animated: true)
self.pageControl.currentPage = self.currenPageNumber
self.currenPageNumber += 1
if self.currenPageNumber == 3 {
self.currenPageNumber = 0
}
}
// 滑動圖片切頁所會觸發的事件
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// 利用scrollView的容器內X座標算出UIPageController要顯示第幾頁
let currentPage: CGFloat = scrollView.contentOffset.x / self.view.frame.size.width
self.pageControl.currentPage = Int(currentPage)
}
override func viewDidLoad() {
super.viewDidLoad()
// JSON格式資料網址
let url: URL = URL(string: "http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=201d8ae8-dffc-4d17-ae1f-e58d8a95b162")!
// 建立session設定
let sessionWithConfigure = URLSessionConfiguration.default
// 設定委任對象為自己
// 下載完成觸發的Delegate
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
// 將取得的資料轉型為JSON格式
let dataDic = try JSONSerialization.jsonObject(with: NSData(contentsOf: location as URL)! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:[String:AnyObject]]
// 將Dictionary存放在陣列裡
self.dataArray = dataDic["result"]!["results"] as! [AnyObject]
// 印出取回的資料
override func viewDidLoad() {
self.view.backgroundColor = UIColor.white
// 生成圖片顯示框
self.imageView = UIImageView()
self.imageView.frame = CGRect(x: self.view.frame.size.width / 4 / 2, y: 64, width: self.view.frame.size.width / 4 * 3, height: self.view.frame.size.height / 4 * 2)
self.imageView.image = UIImage(named: "profile")
// 生成相機按鈕
let cameraBtn: UIButton = UIButton()
/// 開啟相機或相簿
///
/// - Parameter kind: 1=相機,2=相簿
func callGetPhoneWithKind(_ kind: Int) {
let picker: UIImagePickerController = UIImagePickerController()
switch kind {
case 1:
// 開啟相機
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
picker.sourceType = UIImagePickerControllerSourceType.camera
/// 取得選取後的照片
///
/// - Parameters:
/// - picker: pivker
/// - info: info
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil) // 關掉
self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 從Dictionary取出原始圖檔
}