控件
一.都是继承UIControl ,可以addTarget()
forControlEvents: .ValueChanged
1.UISegmentedControl分段控件
提供单选能力
momentary(Bool) 是否记录状态
2.UISlide滑动条
maxmumValue/min....最大值,最小值
value 当前值
continuous 是否连续触发
3.Switch 开关
on (Bool) 是否是开启的状态
4.UIPageControl 分页
与上面的类似
二.PickerView :
与tableview类似
let pickerView = UIPickerView(frame: CGRect(x: 0, y: 100, width: self.view.bounds.size.width, height: 200))
pickerView.dataSource = self
pickerView.delegate = self
self.view.addSubview(pickerView)
pickerView:
1.pickerViewDatasource
func 多少comp
func 多少row
2.pickerViewDelegate
func title
func pickerView view可以重用
方法有的不是必须实现
//列数
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 4
}
// func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// return "a"
// }
//可以重用
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
if let v = view {
print("reusing")
v.backgroundColor = UIColor.redColor()
return v
}
else {
print("no reuse")
let greenView = UIView()
greenView.backgroundColor = UIColor.greenColor()
return greenView
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("\(component): \(row)")
}
UIDatePicker 与 pickerview类似,但是实现起来简单
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 300, width: self.view.bounds.size.width, height: 200))
// datePicker.datePickerMode = .Date
datePicker.datePickerMode = .Time
datePicker.addTarget(self, action: #selector(didChange(_:)), forControlEvents: .ValueChanged)
self.view.addSubview(datePicker)
func didChange(sender: UIDatePicker) {
print(sender.date) //NSDate
}
DatePick:
let datePick = UIDatePicker(frame: CGRect(x: 0, y: 300, width: self.view.frame.width, height: 100))
datePick.addTarget(self, action: #selector(didDate(_:)), forControlEvents: .ValueChanged)
datePick.datePickerMode = .DateAndTime
//.Time//时:分:上午/下午
//.Date//日期:时:分//.DateAndTime
//日期:时:分:上午/下午
//.CountDownTimer //小时:分钟
三.UIWebView
let webView = UIWebView(frame: self.view.bounds)
webView.delegate = self
// let url = NSURL(string: "http://ifeng.com")
// let request = NSURLRequest(URL: url!)
// webView.loadRequest(request)
// let htmlStr = "<h1>这是一个标题</h1>"
// webView.loadHTMLString(htmlStr, baseURL: nil)
//1. 加载本地网页
let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html")
// let request = NSURLRequest(URL: url!)
// webView.loadRequest(request)
//2.
let data = NSData(contentsOfURL: url!)
webView.loadData(data!, MIMEType: "text/html", textEncodingName: "utf-8", baseURL: NSURL())
self.view.addSubview(webView)
//1. 开始加载的时机
//2. 可以过滤网址
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let path = request.URL?.absoluteString
print(path!)
if path!.containsString("baidu.com") {
return false
}
return true
}
func webViewDidStartLoad(webView: UIWebView) {
//
}
func webViewDidFinishLoad(webView: UIWebView) {
//如果需要操作网页,必须等加载完成
let res = webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('img')")
print(res)
//w3school.com.cn
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
}
这个控件还没怎么使用暂时先mark下来
四UI( )GestureRecognizer
tap,screenEdgePanges,swipe,longPress等
let tap = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
self.view.addGestureRecognizer(tap)
let screenGes = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(scGes(_:)))
self.view.addGestureRecognizer(screenGes)
screenGes.edges = UIRectEdge.Left
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
self.view.addGestureRecognizer(tapGesture)
let swipe1 = UISwipeGestureRecognizer(target: self, action: #selector(swipe01(_:)))
swipe1.direction = .Down
self.view.addGestureRecognizer(swipe1)
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(swipe02(_:)))
self.view.addGestureRecognizer(longPress)
五UIAlertController
继承ViewController,也是一个页面,所以显示可以用present
let alCtrl = UIAlertController(title: "警告", message: "别瞎点", preferredStyle: .Alert)
let action1 = UIAlertAction(title: "返回", style: .Cancel) { (action) in
let textfield = alCtrl.textFields![0]
print(textfield.text)
}
alCtrl.addAction(action1)
alCtrl.addTextFieldWithConfigurationHandler { (textf) in
}
self.presentViewController(alCtrl, animated: true, completion: nil)
AlertController页面里的输入框输入的文字可以通过Action的闭包传回来,这是使用输入文字的一个时机
网友评论