1,button
let btn1 = UIButton(frame: CGRect(x: 40, y: 80, width: 100, height: 40))
btn1.backgroundColor = UIColor.green
btn1.setTitle("button", for: .normal)
btn1.addTarget(self, action: #selector(nextPage), for: UIControlEvents.touchUpInside)
//边框效果
btn1.layer.cornerRadius = 8
btn1.layer.masksToBounds = true
self.view.addSubview(btn1)
@objc func nextPage(){
let vc = SecondViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
2,label
let label = UILabel(frame: CGRect(x: 20, y: 150, width: 200, height: 60))
label.text = "hello,Xcode and Swift"
label.font = UIFont(name: "Arial", size: 20)
//标签文字的阴影颜色
label.shadowColor = UIColor.lightGray
//阴影横纵向的偏移距离
label.shadowOffset = CGSize(width: 3, height: 3)
label.textAlignment = NSTextAlignment.center
label.textColor = UIColor.red
label.backgroundColor = UIColor.yellow
//换行方式,以空格为例
// label.lineBreakMode = NSLineBreakMode.byWordWrapping
// label.numberOfLines = 0
self.view.addSubview(label)
3, view
let view1 = UIView(frame: CGRect(x: 40, y: 120, width: 200, height:200))
view1.backgroundColor = UIColor.brown;
//纹理填充
// let image = UIImage(named: "2")
// let patternColor1 = UIColor.init(patternImage: image!)
// view1.backgroundColor = patternColor1;
//仿射变换
var transform = view1.transform
//对视图进行45度旋转
transform = transform.rotated(by: 3.14/4)
view1.transform = transform
// let view2 = UIView(frame: CGRect(x: 40, y: 340, width: 100, height: 100))
let view2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view2.backgroundColor = UIColor.purple;
//渐变填充
//1.新建一个渐变图层
let gradientLayer = CAGradientLayer()
gradientLayer.frame = view2.frame
//2.设置渐变的起始颜色
let fromColor = UIColor.purple.cgColor
//3.设置中间颜色
let midColor = UIColor.red.cgColor
//4.设置结束颜色
let toColor = UIColor.yellow.cgColor
//5.将渐变层的颜色数组属性,设置为数组
gradientLayer.colors = [fromColor,midColor,toColor]
//6.将渐变图层添加到视图中
view2.layer.addSublayer(gradientLayer)
self.view.addSubview(view1)
// self.view.addSubview(view2)
view1.addSubview(view2);
4, imageView
let imageView = UIImageView(image: UIImage(named: "2"))
imageView.frame = CGRect(x: 40, y: 340, width: 100, height: 100)
imageView.layer.cornerRadius = 50
// imageView.layer.masksToBounds = true
//添加阴影效果
imageView.layer.shadowColor = UIColor.red.cgColor
//阴影的横向纵向偏移值
imageView.layer.shadowOffset = CGSize(width: 10, height: 10)
//阴影透明度
imageView.layer.shadowOpacity = 0.8
//阴影半径大小
imageView.layer.shadowRadius = 5
imageView.isUserInteractionEnabled = true
//手势单击
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTag))
imageView.addGestureRecognizer(tapGesture)
self.view.addSubview(imageView)
5, switch , alert
let uiSwitch = UISwitch(frame: CGRect(x: 270, y: 80, width: 80, height: 40))
uiSwitch.setOn(true, animated: true)
uiSwitch.addTarget(self, action: #selector(switchChanged(uiSwitch:)), for: .valueChanged)
self.view.addSubview(uiSwitch)
@objc func switchChanged(uiSwitch:UISwitch){
var message = "Turn on the switch"
if !uiSwitch.isOn {
message = "Turn off the switch"
}
let alert = UIAlertController(title: "alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
6, stepper 步进对象
let label1 = UILabel(frame: CGRect(x: 250, y: 165, width: 40, height: 30))
label1.backgroundColor = UIColor.green
label1.textAlignment = NSTextAlignment.center
label1.text = "0"
label1.tag = 1
self.view.addSubview(label1)
let stepper = UIStepper(frame: CGRect(x: 300, y: 165, width: 0, height: 0))
stepper.backgroundColor = UIColor.lightGray
stepper.sizeToFit()
stepper.value = 0
stepper.minimumValue = 0
stepper.maximumValue = 10
stepper.stepValue = 1
stepper.addTarget(self, action: #selector(FirstViewController.valueChanged(stepper:)), for: .valueChanged)
self.view.addSubview(stepper)
@objc func valueChanged(stepper:UIStepper){
let value = stepper.value
let label = self.view.viewWithTag(1) as! UILabel
label.text = "\(value)"
}
7,textfield
let field = UITextField(frame: CGRect(x: 20, y: 240, width: 150, height: 40))
field.borderStyle = UITextBorderStyle.roundedRect
field.placeholder = "请输入...."
//关闭文本框对象的语法错误提示功能
field.autocorrectionType = UITextAutocorrectionType.no
field.returnKeyType = UIReturnKeyType.done
field.clearButtonMode = UITextFieldViewMode.whileEditing
field.keyboardType = UIKeyboardType.emailAddress
//文本框的键盘为暗色主题
field.keyboardAppearance = UIKeyboardAppearance.dark
field.delegate = self
self.view.addSubview(field)
8, 从下向上弹出的选择提示框
let alert = UIAlertController(title: "Infomation", message: "what's your favorate?", preferredStyle: .actionSheet)
let fishing = UIAlertAction(title: "Fishing", style: .default) { (alerts:UIAlertAction) in
print("I like fishing")
}
let hunting = UIAlertAction(title: "Hunting", style: .destructive) { (alerts:UIAlertAction) in
print("I like hunting")
}
let nothing = UIAlertAction(title: "Nothing", style: .cancel) { (alerts:UIAlertAction) -> Void in
print("A Life of Nonsense")
}
alert.addAction(fishing)
alert.addAction(hunting)
alert.addAction(nothing)
self.present(alert, animated: true, completion: nil)
9,环形活动指示器
//首先添加一个全局 环形进度条对象,作为当前视图控制器类的属性
var indicater:UIActivityIndicatorView!
var p = true
let indicateBtn = UIButton()
indicater.startAnimating()
self.view.addSubview(indicater)
indicateBtn.frame = CGRect(x: 40, y: 320, width: 100, height: 40)
indicateBtn.setTitle("stop", for: .normal)
indicateBtn.backgroundColor = UIColor.orange
indicateBtn.addTarget(self, action: #selector(FirstViewController.stopAnimation), for: UIControlEvents.touchUpInside)
self.view.addSubview(indicateBtn)
@objc func stopAnimation(){
if p == true {
p = !p
indicater.stopAnimating()
indicateBtn.setTitle("start", for: .normal)
}else{
p = !p
indicater.startAnimating()
indicateBtn.setTitle("stop", for: .normal)
}
}
10,datePicker
let datePicker = UIDatePicker()
datePicker.center = CGPoint(x: 200, y: 200)
datePicker.tag = 1
self.view.addSubview(datePicker)
let rect = CGRect(x: 50, y: 360, width: 280, height: 44)
let button = UIButton(type: .roundedRect)
button.frame = rect
button.backgroundColor = UIColor.lightGray
button.setTitle("get date", for: UIControlState())
button.addTarget(self, action: #selector(SecondViewController.getDate), for: .touchUpInside)
self.view.addSubview(button)
//MARK: - button 点击
@objc func getDate(){
let datePicker = self.view.viewWithTag(1) as! UIDatePicker
//获取日期拾取的日期值
let date = datePicker.date
//新建一个日期格式化对象,用来实现日期的格式化
let dateFormater = DateFormatter()
//设置日期的格式
dateFormater.dateFormat = "yyyy-MM-dd HH:mm"
//将日期转换为指定格式的字符串
let dateAndTime = dateFormater.string(from: date)
let alert = UIAlertController(title: "information", message: dateAndTime, preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
}
@objc func nextPage(){
self.navigationController?.popViewController(animated: true)
}
11,webview
let webView = UIWebView(frame: CGRect(x: 20, y: 80, width: 375, height: 200))
let url = URL(string: "https://www.baidu.com")
let request = URLRequest(url: url!)
webView.loadRequest(request)
self.view.addSubview(webView)
12, mkMapView
class ThirdViewController: UIViewController,MKMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.purple
//加载简单地图
// self.mkMapView()
//加载指定地理坐标的地图
// self.mkMapView2()
// //在地图上显示提示框
self.mkMapView3()
}
@objc func mkMapView(){
let map = MKMapView(frame: CGRect(x: 20, y: 300, width: 375, height: 250))
//在地图中显示当前用户的地理位置
map.showsUserLocation = true
//设置地图视图的简单样式,为卫星图模式
map.mapType = MKMapType.satellite
self.view.addSubview(map)
}
@objc func mkMapView2(){
let map = MKMapView(frame: CGRect(x: 20, y: 300, width: 375, height: 250))
//在地图中显示当前用户的地理位置
map.showsUserLocation = true
//设置地图视图的显示样式,为卫星图样式
map.mapType = MKMapType.satellite
//通过设置经纬度来创建一个地理位置
let coordingte2D = CLLocationCoordinate2D(latitude: 39.915352, longitude: 116.397105)
//设置地图显示区域的缩放级别
let zoomLevel = 0.02
//初始化一个变量,表示地图对象的显示区域
let region = MKCoordinateRegionMake(coordingte2D, MKCoordinateSpanMake(zoomLevel, zoomLevel))
//设置地图对象的显示区域
map.setRegion(map.regionThatFits(region), animated: true)
self.view.addSubview(map)
}
@objc func mkMapView3(){
let map = MKMapView(frame: CGRect(x: 20, y: 300, width: 375, height: 250))
//在地图中显示当前用户的地理位置
map.showsUserLocation = true
//设置地图视图的显示样式,为卫星图样式
map.mapType = MKMapType.satellite
//通过设置经纬度来创建一个地理位置
let coordingte2D = CLLocationCoordinate2D(latitude: 39.915352, longitude: 116.397105)
//设置地图显示区域的缩放级别
let zoomLevel = 0.02
//初始化一个变量,表示地图对象的显示区域
let region = MKCoordinateRegionMake(coordingte2D, MKCoordinateSpanMake(zoomLevel, zoomLevel))
//设置地图对象的显示区域
map.setRegion(map.regionThatFits(region), animated: true)
//初始化一个点注释对象
let objectAnnotation = MKPointAnnotation()
//设置注释对象的地理位置
objectAnnotation.coordinate = coordingte2D
//设置注释对象的标题内容
objectAnnotation.title = "Imperial Palace"
//设置注释对象的子标题内容
objectAnnotation.subtitle = "China's biggest palace."
//将注释窗口对象,添加到地图中
map.addAnnotation(objectAnnotation)
self.view.addSubview(map)
}
13, tableView
import UIKit
class ForeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
let array:[String] = ["我是谁","我从哪里来","要到哪里去"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.gray
self.tableView()
}
@objc func tableView(){
let tableView = UITableView(frame: CGRect(x: 20, y: 80, width: 375, height: 150),style:UITableViewStyle.plain)
tableView.backgroundColor = UIColor.white
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellid")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath)
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cellid")
}
cell.textLabel?.text = array[indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}