第八天的作业练习
作业一(创建三个滑竿来改变视图的背景色)
import UIKit
class ViewController: UIViewController {
//定义为全局变量
var sliderRed:UISlider! = nil
var sliderGreen:UISlider! = nil
var sliderBlue:UISlider! = nil
override func viewDidLoad() {
super.viewDidLoad()
sliderRed = UISlider(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
sliderRed.maximumValue = 255
sliderRed.minimumValue = 0
sliderRed.thumbTintColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
self.view.addSubview(sliderRed)
sliderRed.addTarget(self, action: #selector(sliderAction), for: .valueChanged)
sliderGreen = UISlider(frame: CGRect(x: 50, y: 150, width: 200, height: 30))
sliderGreen.addTarget(self, action: #selector(sliderAction), for: .valueChanged)
sliderGreen.maximumValue = 255
sliderGreen.minimumValue = 0
sliderGreen.thumbTintColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
self.view.addSubview(sliderGreen)
sliderBlue = UISlider(frame: CGRect(x: 50, y: 200, width: 200, height: 30))
sliderBlue.addTarget(self, action: #selector(sliderAction), for: .valueChanged)
sliderBlue.thumbTintColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
sliderBlue.maximumValue = 255
sliderBlue.minimumValue = 0
self.view.addSubview(sliderBlue)
}
func sliderAction(slider:UISlider){
let color = UIColor(colorLiteralRed: sliderRed.value/255, green: sliderGreen.value/255, blue: sliderBlue.value/255, alpha: 1.0)
self.view.backgroundColor = color
}
}
**运行结果如下图所示,滑动滑竿可以改变view的背景色
作业二(创建一个Book类, Book中有书名和书的内容两个属性, 实现手势切换)
import UIKit
class Book: NSObject {
var titleName:String?
var content:String?
init(titleName:String,content:String) {
self.titleName = titleName
self.content = content
}
}
class ViewController: UIViewController {
var bookArr = [Book]()//书的数组
var titleLabel:UILabel! = nil
var contentLabel:UILabel! = nil
var index:Int = 0 //显示第0本书
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...5{
let book = Book(titleName: "第\(index)篇文章", content: "第\(index)篇文章的内容")
self.bookArr.append(book)
}
//初始化手势
let swip = UISwipeGestureRecognizer()
swip.direction = .left
self.view.addGestureRecognizer(swip)
swip.addTarget(self, action: #selector(swipAction(swip:)))
let swip1 = UISwipeGestureRecognizer()
swip1.direction = .right
self.view.addGestureRecognizer(swip1)
swip1.addTarget(self, action: #selector(swipAction(swip:)))
//添加两个label
self.titleLabel = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
self.view.addSubview(titleLabel)
self.contentLabel = UILabel(frame: CGRect(x: 100, y: 200, width: 300, height: 40))
self.view.addSubview(contentLabel)
//加载内容
//先获取第0本书
let currentBook = bookArr[index]
self.titleLabel.text = currentBook.titleName
self.contentLabel.text = currentBook.content
}
func swipAction(swip:UISwipeGestureRecognizer){
//判断方向
if swip.direction == .right {
index-=1
if index < 0 {
index = bookArr.count - 1
}
} else if swip.direction == .left {
index+=1
if index >= bookArr.count {
index = 0
}
}
let currentBook = bookArr[index]
self.titleLabel.text = currentBook.titleName
self.contentLabel.text = currentBook.content
}
}
**运行结果如下图所示
作业三(注册界面)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var label = UILabel(frame: CGRect(x: 100, y: 50, width: 100, height: 40))
label.text = "注册"
label.textColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
label.textAlignment = .center
self.view.addSubview(label)
var usename = UITextField(frame: CGRect(x: 50, y: 100, width: 200, height: 50))
usename.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
usename.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
usename.leftView = UIImageView(image: UIImage(named: "xr.png"))
usename.leftViewMode = .always
self.view.addSubview(usename)
usename.placeholder = "请输入用户名"
//编辑时出现(❌)
usename.clearButtonMode = .whileEditing
usename.borderStyle = .roundedRect
var password = UITextField(frame: CGRect(x: 50, y: 180, width: 200, height: 50))
password.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
password.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
password.leftView = UIImageView(image: UIImage(named: "yaoshi.jpg"))
password.leftViewMode = .always
self.view.addSubview(password)
password.placeholder = "请输入密码"
password.borderStyle = .roundedRect
password.clearButtonMode = .whileEditing
var btn = UIButton(type: .system)
btn.addTarget(self, action: #selector(btnAction(btn:)), for: .valueChanged)
btn.setTitle("注册", for: .normal)
btn.frame = CGRect(x: 200, y: 240, width: 50, height: 50)
self.view.addSubview(btn)
var enter = UIButton(type: .system)
enter.addTarget(self, action: #selector(enterAction(enter:)), for: .valueChanged)
enter.frame = CGRect(x: 60, y: 240, width: 50, height: 50)
enter.setTitle("登录", for: .normal)
self.view.addSubview(enter)
}
func btnAction(btn:UIButton){
print("注册成功")
}
func enterAction(enter:UIButton){
print("登录成功")
}
}
**运行结果如下图所示
网友评论