课程总结
跑马灯(练习timer)
//
// ViewController.swift
// 走马灯
//
// Created by s20191106196 on 2020/12/2.
// Copyright © 2020 s20191106196. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var box1: UIButton!
@IBOutlet weak var box3: UIButton!
@IBOutlet weak var box2: UIButton!
@IBOutlet weak var box5: UIButton!
@IBOutlet weak var box4: UIButton!
var timer = Timer()//定义一个时间变量
var boxes:[UIButton] = []//定义一个UIBUTTOM的数组
var count=0
var flag = 1,item=1
//主题思路是通过flag的正负暂停和开始
@IBAction func start(_ sender: Any) {
flag = -flag
}
override func viewDidLoad() {
super.viewDidLoad()
boxes.append(box1)//将按钮放入数组
boxes.append(box2)
boxes.append(box3)
boxes.append(box4)
boxes.append(box5)
// Do any additional setup after loading the view.
//将timer放入方法中不会乱速,withTimeInterval是时间,block后要加_ in
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block:{_ in
if self.flag == -1 {
for box in self.boxes{
box.backgroundColor = UIColor.green
}
self.boxes[self.count].backgroundColor = UIColor.red
//往返跑item为标志
if self.count == 4 {
self.item = -1
}
if self.count == 0 {
self.item = 1
}
if self.item == 1{
self.count = self.count + 1
}
if self.item == -1{
self.count = self.count - 1
}
}
})
}
}
第二版
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var box1: UIButton!
@IBOutlet weak var box5: UIButton!
@IBOutlet weak var box4: UIButton!
@IBOutlet weak var box3: UIButton!
@IBOutlet weak var box2: UIButton!
var timer = Timer()
var boxes:[UIButton] = []
var count=0,flag=1,item=1
@IBAction func start(_ sender: Any) {
if flag==1{
timer.fireDate=Date.distantPast
}
else {
timer.fireDate = Date.distantFuture
}
flag = -flag
}
override func viewDidLoad() {
super.viewDidLoad()
boxes.append(box1)
boxes.append(box2)
boxes.append(box3)
boxes.append(box4)
boxes.append(box5)
// Do any additional setup after loading the view.
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block:{_ in
for box in self.boxes{
box.backgroundColor = UIColor.green
}
self.boxes[self.count].backgroundColor = UIColor.red
if self.count == 4 {
self.item = -1
}
if self.count == 0 {
self.item = 1
}
if self.item == 1{
self.count = self.count + 1
}
if self.item == -1{
self.count = self.count - 1
}
})
timer.fireDate = Date.distantFuture
}
}
答题小游戏
1.弹窗要按插件
import UIKit
class ViewController: UIViewController {
var current = 0
var score = 0
let question = [
"魔镜魔镜,你啥时候碎呢?"
,"魔镜魔镜,我啥时候能有钱?","魔镜魔镜,你说你值多少钱?","魔镜魔镜,咋俩摆个地摊吧,照一次5块行不行?","魔镜魔镜,你活多久了?"
]
let answers =
["你照镜子的时候","做梦的时候","你干嘛","别在我身上打主意","不知道,但是见到你,我活不过今天了"]
let noanswers =
["你手别抖","不久","我很贵哦","好呀","很久"]
@IBOutlet weak var jindutiao: UIView!
@IBOutlet weak var mojing: UILabel!
@IBOutlet weak var no: UILabel!
@IBOutlet weak var yes: UILabel!
@IBOutlet weak var change: UILabel!
@IBOutlet weak var defen: UILabel!
@IBAction func ture(_ a: UIButton) {
score = score + 1
current = current + 1
defen.text = "总得分\(score)";
yes.text=answers[current]
mojing.text=question[current]
no.text=noanswers[current]
jindutiao.frame.size.width=view.frame.width*(CGFloat(current+1))/5
change.text="\(current+1)/5"
if current == 4 {
current = -1
score = -1
}
change.text="\(current+1)/5"
ProgressHUD.showSuccess("答对了")
let alert = UIAlertController(title: "提示", message: "是否继续", preferredStyle: .alert)
let ok = UIAlertAction(title: "确定", style: .default, handler: nil)
let nok = UIAlertAction(title: "取消", style: .destructive, handler: nil)
alert.addAction(ok)
alert.addAction(nok)
self.present(alert,animated: true,completion: nil)
}
@IBAction func False(_ b: UIButton) {
current = current + 1
mojing.text=question[current]
yes.text=answers[current]
no.text=noanswers[current]
jindutiao.frame.size.width=view.frame.width*(CGFloat(current+1))/5
change.text="\(current+1)/5"
if current == 4 {
current = -1
score = -1;
}
ProgressHUD.showError("答错了")
let Falert = UIAlertController(title: "提示", message: "是否继续", preferredStyle: .alert)
let no = UIAlertAction(title: "取消", style: .destructive, handler: nil)
Falert.addAction(no)
self.present(Falert,animated: true,completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mojing.text=question[current]
yes.text=answers[current]
no.text=noanswers[current]
}
}
音乐游戏
上传音乐
//
// ViewController.swift
// 音乐游戏
//
// Created by s20191106196 on 2020/10/23.
// Copyright © 2020 s20191106196. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player:AVAudioPlayer!
@IBAction func btn(_ sender: UIButton) {
let url = Bundle.main.url(forResource: "note\(sender.tag)", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
let url = Bundle.main.url(forResource: "note8", withExtension: ".mp3")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
@IBAction func btn2(_ sender: Any) {
let url = Bundle.main.url(forResource: "note2", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
@IBAction func btn3(_ sender: Any) {
let url = Bundle.main.url(forResource: "note3", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
@IBAction func btn4(_ sender: Any) {
let url = Bundle.main.url(forResource: "note4", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
@IBAction func btn5(_ sender: Any) {
let url = Bundle.main.url(forResource: "note5", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
@IBAction func btn6(_ sender: Any) {
let url = Bundle.main.url(forResource: "note6", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
@IBAction func btn7(_ sender: Any) {
let url = Bundle.main.url(forResource: "note7", withExtension: ".wav")
do {
player = try AVAudioPlayer(contentsOf: url!)
}
catch {
print("错误")
}
player.play()
}
*/
}
骰子游戏
//
// ViewController.swift
// 游戏
//
// Created by s20191106196 on 2020/10/21.
// Copyright © 2020 s20191106196. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
let random = Int.random(in: 1...6)
@IBOutlet weak var ITimage: UIImageView!
@IBOutlet weak var TTimage: UIImageView!
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
ITimage.image = UIImage(named: "dice\(Int.random(in: 1...6))")
TTimage.image = UIImage(named: "dice\(Int.random(in:1...6))")
}
@IBAction func change(_ sender: Any) {
ITimage.image = UIImage(named: "dice\(Int.random(in: 1...6))")
TTimage.image = UIImage(named: "dice\(Int.random(in:1...6))")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
密码验证
//
// ViewController.swift
// 10.14
//
// Created by s20191106196 on 2020/10/14.
// Copyright © 2020 s20191106196. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ITimage: UIImageView!
@IBAction func change(_ sender: Any) {
ITimage.image = UIImage(named: "1")
}
@IBOutlet weak var uersname: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var tip: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func yanzheng(_ sender: Any) {
if uersname.text=="admin"&&password.text=="88888888"{
tip.text="登录成功"
}
else{
tip.text="登陆失败"
}
}
}
语法练习
1
//
// main.swift
// 9.16
//
// Created by s20191106196 on 2020/9/16.
// Copyright © 2020 s20191106196. All rights reserved.
//
import Foundation
/*
print("请输入用户名")
var username = readLine()
print("请输入密码")
var password = readLine()
if username != "admin"{
print("用户不存在")
}
else if password == "88888888"{
print("密码正确")
}
else {
print("密码错误!")
}
*/
/*
var index:Int
index = Int(readLine()!)!
switch index {
case 100 :
print( "index 的值为 100")
fallthrough
case 10,15 :
print( "index 的值为 10 或 15")
fallthrough
case 5 :
print( "index 的值为 5")
default :
print( "默认 case")
}
*/
/*
print("请输入您的分数")
var score:Int
score = Int(readLine()!)!
score = score / 10 ;
switch score {
case 10 :
print("A")
case 9 :
print("A")
case 8 :
print("B")
case 7 :
print("C")
case 6 :
print("D")
default :
print("不及格")
}
*/
/*
var index:Int = 0
var sum:Int = 0
for index in 1...100{
sum += index
}
print("1到100的和为: \(sum)")
*/
/*
var key = 0
while key==0 {
print("请输入用户名")
var username = readLine()
print("请输入密码")
var password = readLine()
if username != "admin"{
print("用户不存在")
}
else if password == "88888888"{
print("用户名和密码正确")
key = 1
}
else {
print("密码错误!")
}
}
*/
var index:Int = 0
var sum:Int = 0
for index in 1...100{
if(index % 7 == 0 ){
continue
}
sum += index
}
print(sum)
2
//
// main.swift
// 9.25
//
// Created by s20191106196 on 2020/9/25.
// Copyright © 2020 s20191106196. All rights reserved.
//
import Foundation
/*print("Hello, World!")
var a = [String]()
a.append("apple")
a.append("Amazon")
a.append("Runoob")
a += ["Google"]
for (index,item) in a.enumerated() {
if index % 2 != 0{
print("在\(index)的位置上的值:\(item)")
}
}
print("hello")*/
/*
var someInt = [3,5,7,8]
var a = someInt[0]
for (index,_) in someInt.enumerated() {
if index < someInt.count - 1{
someInt[index] = someInt[index + 1]
}
else {
someInt[index] = a
}
}
print(someInt)*/
/*for item in 1...9{
for index in 1...item {
if index == item{
print("\(index)*\(item)=\(index * item)")
continue
}
print("\(index)*\(item)=\(index * item)",terminator: " ")
}
}*/
//var index = 2
//var someint = [Int]()
//for item in 101...200{
//for index in 2 ..< item / 2 {
// if item % index == 0{
// break
// }
// }}
// if index == item / 2 - 1 {
// print("\(item) ")
// }
//}
//func sum(a:Int,b:Int) -> Int {
// return a+b
//}
//
//var num = sum (a: 10, b: 20)
//print(num)
func findmiddle(a:Int,b:Int,c:Int) -> Int
{
if(a>b && a<c)||(a>c&&a<b){
return a
}
if(b>a && b<c)||(b>c&&b<a){
return b
}
if(c>a && c<b)||(c>b&&c<a){
return a
}
return 0
}
var b = findmiddle(a: 2, b: 4, c: 1)
print(b)
3
//
// main.swift
// 9.29
//
// Created by s20191106196 on 2020/9/30.
// Copyright © 2020 s20191106196. All rights reserved.
//
import Foundation
print("Hello, World!")
/*for num in 101...200{
for num1 in
}*/
var index = 2
var issushu = true
for item in 101...200{
for index in 2 ..< item / 2 {
if item % index == 0{
issushu = false
break
}
}
if issushu == true {
print("\(item) ")
}
issushu = true
}
4
for循环
import Cocoa
print("闭间区间符;")
for index in 1...5{
print("/(index)*5 = \(index * 5)")
}
print("半开区间运算符")
for index in 1..<5{
print("/(index)*5 = \(index * 5)")
}
print(10<5 ? "A":"B")