Skip to content

Instantly share code, notes, and snippets.

View KrauserHuang's full-sized avatar
:bowtie:
Learning iOS right now!

Krauser Huang KrauserHuang

:bowtie:
Learning iOS right now!
  • Taiwan
View GitHub Profile
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)
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) {
@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)
import UIKit
class RandomImageViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var segmentControl: UISegmentedControl!
var urlStr = ""
override func viewDidLoad() {
import UIKit
class CustomeImageViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var urlStr = ""
override func viewDidLoad() {
super.viewDidLoad()
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)
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大小
// 建立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
// 每一組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
// 點選cell後的動作
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("你選擇了第\(indexPath.section)組的")
print("第\(indexPath.item)張圖片")
}