酱油02-小控件和手势

2016-08-18  本文已影响27人  没有北方的南方

小控件的简单使用

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        //Segmented Control
        let title = ["xx", "ss", "qq"]
        let seg = UISegmentedControl(items: title)
        seg.frame = CGRect(x: 100, y: 100, width: 300, height: 100)
        seg.addTarget(self, action: #selector(didchoose(_:)), forControlEvents: .ValueChanged)
        //        seg.momentary = true
        
        self.view.addSubview(seg)
        //Slider
        let slider = UISlider(frame: CGRect(x: 300, y: 300, width: 100, height: 100))
        slider.continuous = false//设置为拖动不计算滑动条的值
        slider.maximumValue = 100
        slider.minimumValue = 0
        slider.addTarget(self, action: #selector(didslider(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(slider)
        
        //Switch
        let swi = UISwitch(frame: CGRect(x: 200, y: 400, width: 100, height: 100))
        swi.onTintColor = UIColor.redColor()
        swi.thumbTintColor = UIColor.greenColor()
        swi.tintColor = UIColor.brownColor()
        
        swi.addTarget(self, action: #selector(didswitch(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(swi)
        //Page Control
        let page = UIPageControl(frame: CGRect(x: 200, y: 300, width: 100, height: 100))
        page.addTarget(self, action: #selector(didpage(_:)), forControlEvents: .ValueChanged)
        page.currentPageIndicatorTintColor = UIColor.redColor()
        page.pageIndicatorTintColor = UIColor.cyanColor()
        page.numberOfPages = 4
        self.view.addSubview(page)
        
        //Stepper
        let ste = UIStepper(frame: CGRect(x: 300, y: 500, width: 100, height: 100))
        ste.maximumValue = 100.00
        ste.minimumValue = 20.00
        ste.addTarget(self, action: #selector(didstep(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(ste)
         // webView
        let url = NSURL(string: "http://www.baidu.com")
        let webview = UIWebView(frame: self.view.bounds)
        let request = NSURLRequest(URL: url!)
        webview.loadRequest(request)
        self.view.addSubview(webview)
    }
    
    //小控件触发的方法
    func didchoose(sender: UISegmentedControl){
        
        print(sender.selectedSegmentIndex)
    }
    
    func didslider(sender: UISlider){
        
        print(sender.value)
    }
    
    func didswitch(sender:UISwitch){
        print(sender.on)
    }
    
    func didpage(sender: UIPageControl){
        print(sender.currentPage)
    }
    
    func didstep(sender: UIStepper){
        print(sender.value)
    }


}

pickerView

import UIKit

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        //创建pike对象,调用协议并显示
        let pike = UIPickerView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 200))
        pike.dataSource = self
        pike.delegate = self
        self.view.addSubview(pike)
        
        //与pike像,但是其左右为显示当前时间
        let date = UIDatePicker(frame: CGRect(x: 0, y: 300, width: self.view.frame.size.width, height: 200))
        
        self.view.addSubview(date)
       }
    //实现协议方法,4列
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 4
    }
    //3行
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 3
    }
    //拖动选择时输出列号,行号
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print(component,row)
    }
    //设置每一个的内容
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "ss"
    }
    //重用,如果为空则创建
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        if let v1 = view{
            v1.backgroundColor = UIColor.redColor()
            return v1
            
        }else{
        
            let v2 = UIView()
            v2.backgroundColor = UIColor.brownColor()
            return v2
        }
    }
}

UIGestureRecognizer/PKHUD插件

import UIKit
import PKHUD//在一些需要等待的时候显示(显示完隐藏)提醒,或是警告


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //单点触碰手势
        let gestrue = UITapGestureRecognizer(target: self, action: #selector(didgesture(_:)))
        HUD.show(.Label("请稍等"))//在显示之前会有提示
        HUD.hide(afterDelay: 3)//在3秒后隐藏
        
        self.view.addGestureRecognizer(gestrue)
    
        
        //滑动手势,上下左右
        let left = UISwipeGestureRecognizer(target: self, action: #selector(didswipe(_:)))
        left.direction = .Left
        self.view.addGestureRecognizer(left)
        
        let right = UISwipeGestureRecognizer(target: self, action: #selector(didswipe1(_:)))
        right.direction = .Right
        self.view.addGestureRecognizer(right)
        
        let up = UISwipeGestureRecognizer(target: self, action: #selector(didswipe3(_:)))
        up.direction = .Up
        self.view.addGestureRecognizer(up)
        
        let down = UISwipeGestureRecognizer(target: self, action: #selector(didswipe2(_:)))
        down.direction = .Down
        self.view.addGestureRecognizer(down)
        
    }
    //单点触碰手势触发方法
    func didgesture (sender: UIGestureRecognizer){
    
        //触碰位置
        let location = sender.locationInView(self.view)
        print(location)
        //设置提示类对象,并设置类型/标题/信息
        let alercrl = UIAlertController(title: "瞅啥瞅", message: "你丑", preferredStyle: .Alert)
        
        //设置提示的文本框与键盘类型
        alercrl.addTextFieldWithConfigurationHandler { (textfile) in
            textfile.placeholder = "bingo"
            textfile.keyboardType = .Default
        }
        
        //设置点击动作
        let action = UIAlertAction(title: "ok", style: .Default) { (action) in
            print("ok")
            let a = alercrl.textFields![0]
            print(a.text!)
        }
        let action1 = UIAlertAction(title: "cancel", style: .Cancel) { (action1) in
            print("cancel")
        }
        //在提示控制器中添加点击动作
        alercrl.addAction(action)
        alercrl.addAction(action1)
        
        self.presentViewController(alercrl, animated: true, completion: nil)
    
    }
    
    //上下左右触发的事件
    func didswipe(sender: UISwipeGestureRecognizer){
    
        print("left")
    }
    func didswipe1(sender: UISwipeGestureRecognizer){
        
        print("right")
    }
    func didswipe2(sender: UISwipeGestureRecognizer){
        
        print("down")
    }
    func didswipe3(sender: UISwipeGestureRecognizer){
        
        print("up")
    }
   
}
上一篇下一篇

猜你喜欢

热点阅读