This file contains hidden or 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 checkPassword() { | |
if typePassword == correctPassword { | |
let controller = UIAlertController(title: "Correct", message: "Welcome back!", preferredStyle: .alert) | |
let action = UIAlertAction(title: "OK", style: .default) { (_) in | |
self.reset() | |
} | |
controller.addAction(action) | |
present(controller, animated: true, completion: nil) | |
} else { | |
let controller = UIAlertController(title: "Wrong", message: "Please try again!", preferredStyle: .alert) |
This file contains hidden or 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 checkAnswer() { | |
if typeTextField.text == String(questionNumber) { | |
let controller = UIAlertController(title: "答對了", message: "正確答案是\(questionNumber)", preferredStyle: .alert) | |
let action = UIAlertAction(title: "Re-Play", style: .default) { (_) in | |
self.playAgain() | |
} | |
controller.addAction(action) | |
present(controller, animated: true, completion: nil) | |
} else if typeTextField.text! > String(questionNumber) { | |
if typeTextField.text! >= String(upperBound) { |
This file contains hidden or 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 go(_ sender: UIButton) { | |
if typeTextField.text == "" { | |
let controller = UIAlertController(title: "請給我一個數字", message: "輸入數字才可以繼續喔!", preferredStyle: .alert) | |
let action = UIAlertAction(title: "了解", style: .default, handler: nil) | |
controller.addAction(action) | |
present(controller, animated: true, completion: nil) | |
} else if chanceNumber > 1 { | |
checkAnswer() | |
} else { | |
let controller = UIAlertController(title: "遊戲結束", message: "正確答案是\(questionNumber)", preferredStyle: .alert) |
This file contains hidden or 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 UIKit | |
class RandomImageViewController: UIViewController { | |
@IBOutlet weak var imageView: UIImageView! | |
@IBOutlet weak var segmentControl: UISegmentedControl! | |
var urlStr = "" | |
override func viewDidLoad() { |
This file contains hidden or 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 UIKit | |
class CustomeImageViewController: UIViewController { | |
@IBOutlet weak var imageView: UIImageView! | |
var urlStr = "" | |
override func viewDidLoad() { | |
super.viewDidLoad() |
This file contains hidden or 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 fetchUserData() { | |
let urlStr = "https://randomuser.me/api/" | |
if let url = URL(string: urlStr) { | |
URLSession.shared.dataTask(with: url) { (data, response, error) in | |
if let data = data, | |
let content = String(data: data, encoding: .utf8) { | |
print(content) | |
let decoder = JSONDecoder() | |
do { | |
let result = try decoder.decode(UserData.self, from: data) |
This file contains hidden or 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
fullScreenSize = UIScreen.main.bounds.size | |
view.backgroundColor = .white | |
// 建立UICollectionViewFloatLayout | |
let layout = UICollectionViewFlowLayout() | |
layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) | |
// 設定cell上下間距 | |
layout.minimumLineSpacing = 5 | |
// 設定cell尺寸 | |
layout.itemSize = CGSize(width: fullScreenSize.width / 3 - 10, height: fullScreenSize.width / 3 - 10) | |
// 設定header/footer大小 |
This file contains hidden or 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
// 建立UICollectionView | |
let myCollectionView = UICollectionView(frame: CGRect(x: 0, y: 20, width: fullScreenSize.width, height: fullScreenSize.height - 20), collectionViewLayout: layout) | |
// 註冊cell來重複利用 | |
myCollectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "Cell") | |
// 註冊section的header/footer來重複使用 | |
myCollectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "Header") | |
myCollectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "Footer") | |
// 設置委任對象 | |
myCollectionView.delegate = self | |
myCollectionView.dataSource = self |
This file contains hidden or 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
// 每一組collectionView有幾個cell | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return 7 | |
} | |
// 每個cell要顯示的內容 | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell | |
cell.imageView.image = UIImage(named: "photo\(indexPath.item)") | |
cell.titleLabel.text = "\(indexPath.item)" | |
return cell |
This file contains hidden or 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
// 點選cell後的動作 | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
print("你選擇了第\(indexPath.section)組的") | |
print("第\(indexPath.item)張圖片") | |
} |