swift5入门练手走起

絮叨:由于2017年花99刀买了苹果的开发者证书,直至到期也没成功上线一款app(由于工作忙的原因,苹果上线审核严格的原因等等~先给自己的失败找些理由,哈哈~~~)

言归正传:今年吸取了教训,先把基础打牢,系统的学习一下swift5的语法和开发技能,先写个练手demo开始。

第一步:搭建ios开发环境(略过,不会的同学请自行百度)

第二步:熟悉swift5的语法

第三步:策划一款小游戏(石头剪子布),自导自研

第四步:show me the code:

1、新建一个ViewController.swift类,用于控制mainboard上的标签和按钮的展示和事件

//
//  ViewController.swift
//  Gaowh
//
//  Created by gaowenhui on 07.06.2020.
//  Copyright © 2020 gaowenhui All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // MARK: - Variables
    var myPoints = 0
    var pcPoints = 0
    var miePoints = 0
    let array = ["tas","kagit","makas"] // 已创建字符串类型的字符串。
    
    // MARK: - IBOutlets
    @IBOutlet weak var myChoiseLabel: UILabel!
    @IBOutlet weak var pcChoiseLabel: UILabel!
    @IBOutlet weak var theEndLabel: UILabel!
    @IBOutlet weak var myPointsLabel: UILabel!
    @IBOutlet weak var pcPointsLabel: UILabel!
    @IBOutlet weak var miePointsLabel: UILabel!
    
    // mie触发的事件: - IBActions
    @IBAction func mieSelected(_ sender: Any) {
        //miePointsLabel.text = " 咩~ "
        miePoints += 1
        var str = "Hello"
         str = str + " gaowenhui"
         str = "\(str)\(miePoints),test,\(200)"
        print(str)
        
        miePointsLabel.text = String(str)
    }
    
    // MARK: - IBActions
    @IBAction func rockSelected(_ sender: Any) {
        
        let randomSelected = Int(arc4random_uniform(3)) // 0,1,2
        let pcSelected = array[randomSelected] // 目录中的随机元素。
        
        if pcSelected == "tas" {
            myChoiseLabel.text = "你的选择是:锤头。"
            pcChoiseLabel.text = "电脑选择是:锤头"
            theEndLabel.text = "平手"
            
            
        }else if pcSelected == "kagit" {
            myChoiseLabel.text = "你的选择是:锤头。"
            pcChoiseLabel.text = "计算机选择:包袱"
            theEndLabel.text = "电脑赢了。"
            
            pcPoints += 1
            
            pcPointsLabel.text = String(pcPoints)
            
        }else if pcSelected == "makas" {
            myChoiseLabel.text = "你的选择是:锤头。"
            pcChoiseLabel.text = "计算机选择:剪刀"
            theEndLabel.text = "你赢了。"
            
            myPoints += 1
            
            myPointsLabel.text = String(myPoints)
            
        }
    }
    
    
    // 文书工作
    @IBAction func paperSelected(_ sender: Any) {
        
        let randomSelected = Int(arc4random_uniform(3)) // 0,1,2
        let pcSelected = array[randomSelected] // 目录中的随机元素。
        
        if pcSelected == "tas" {
            myChoiseLabel.text = "你的选择是:包袱"
            pcChoiseLabel.text = "电脑选择:锤头"
            theEndLabel.text = "你赢了。"
            
            myPoints += 1
            
            myPointsLabel.text = String(myPoints)
            
            
        }else if pcSelected == "kagit" {
            myChoiseLabel.text = "你的选择是:包袱"
            pcChoiseLabel.text = "计算机选择:包袱"
            theEndLabel.text = "你们打平。"
            
        
            
        }else if pcSelected == "makas" {
            myChoiseLabel.text = "你的选择是:包袱"
            pcChoiseLabel.text = "计算机选择:剪刀"
            theEndLabel.text = "电脑赢了。"
            
            pcPoints += 1
            
            pcPointsLabel.text = String(pcPoints)
            
        }
        
    }
    
    // MACE相关程序
    @IBAction func scissorSelected(_ sender: Any) {
        
        let randomSelected = Int(arc4random_uniform(3)) // 0,1,2
        let pcSelected = array[randomSelected] // 目录中的随机元素。
        
        if pcSelected == "tas" {
            myChoiseLabel.text = "你的选择是:剪刀"
            pcChoiseLabel.text = "电脑选择:锤头"
            theEndLabel.text = "电脑赢了。"
            
            pcPoints += 1
            
            pcPointsLabel.text = String(pcPoints)
            
            
        }else if pcSelected == "kagit" {
            myChoiseLabel.text = "你的选择是:剪刀"
            pcChoiseLabel.text = "计算机选择:包袱"
            theEndLabel.text = "你赢了。"
            
            myPoints += 1
            
            myPointsLabel.text = String(myPoints)
           
            
        }else if pcSelected == "makas" {
            myChoiseLabel.text = "你的选择是:剪刀"
            pcChoiseLabel.text = "电脑选择:剪刀"
            theEndLabel.text = "打平"
           
            
        }
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 加载视图后,通常从一个笔尖执行任何附加设置
    }




}

2、新建一个代理处理类AppDelegate.swift 继承 UIResponder, UIApplicationDelegate,导入UIKit

//
//  AppDelegate.swift
//
//  Gaowh
//  Created by gaowh on 06.07.2020.
//  Copyright © 2020 gaowh All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

3、给按钮和标签绑定事件

swift5入门练手走起_第1张图片

4、调试运行程序

swift5入门练手走起_第2张图片

swift5入门练手走起_第3张图片

swift5入门练手走起_第4张图片

最后:好了,成功了,通过这个小demo基本打开了一个门外汉通过ios这一扇大门的钥匙,希望能给有兴趣的同学起到抛砖引玉的效果,代码已经传到github,欢迎 star,欢迎 fork

https://github.com/gwh2008/Gaowh

 

你可能感兴趣的:(Swift,swift,xcode,ios)