码迷,mamicode.com
首页 > 编程语言 > 详细

Swift基础之PickerView(时间)选择器

时间:2016-08-02 11:43:03      阅读:2387      评论:0      收藏:0      [点我收藏+]

标签:

代码讲解:(后面有额外代码讲解)

首页设计UIPickerView的样式设计:

leftArray = ["花朵","颜色","形状"];
        
        let array1 = ["茉莉","玫瑰","郁金香","紫罗兰"];
        let array2 = ["红色","黄色","黑色","白色","紫色"];
        let array3 = ["圆形","方形","椭圆形"];
        righArray = [array1,array2,array3];
        
        let pickerV = UIPickerView.init(frame: CGRectMake(0, 100, self.view.frame.size.width, 260));
        //pickerV.backgroundColor = UIColor.brownColor();
        //设置是否显示选中状态
        pickerV.showsSelectionIndicator = true;
        //设置代理
        pickerV.dataSource = self;
        pickerV.delegate = self;
        self.view.addSubview(pickerV);
        
        let btn = UIButton.init(frame: CGRectMake(40, 380, F_Device_W()-40*2, 40));
        btn.setTitle("时间选择器", forState: UIControlState.Normal);
        btn.backgroundColor = UIColor.cyanColor();
        btn.layer.cornerRadius = 5;
        self.view.addSubview(btn);
        btn.addTarget(self, action: #selector(btnClick), forControlEvents:.TouchUpInside);

添加代理,执行必要的代理方法:

//MARK:---必须要实现的UIPickerViewDatasource的方法
    //返回几个分区
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 2;
    }
    //返回各分区行数
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if (component == 0)
        {
            return leftArray.count;
        }
        else
        {
            //获取选中第0分区的哪一行
            let row1 = pickerView.selectedRowInComponent(0);
            //然后根据获取的行号获取第1分区所对应的数据
            let arrarY = righArray.objectAtIndex(row1);
            return arrarY.count;
        }
    }

时间选择器UIDatePicker

//UIDatePicker时间选择器,继承于UIControl
        let datePick = UIDatePicker.init(frame: CGRectMake(0, 80, self.view.frame.size.width, 300));
        //设置显示时间样式,中or英,默认英文显示样式
        datePick.locale = NSLocale.init(localeIdentifier: "zh_CN");//中文
        datePick.backgroundColor = UIColor.lightGrayColor();
        //设置显示类型
        datePick.datePickerMode = UIDatePickerMode.DateAndTime;//日期和时间
        //设置默认的日期
        datePick.date = NSDate.init();
        //设置可以选择的最小日期
        datePick.minimumDate = NSDate.init(timeIntervalSinceNow: -(60*60*24*7));
        //设置可以选择的最大日期
        datePick.maximumDate = NSDate.init(timeIntervalSinceNow: 60*60*24*7);
        //添加方法获取显示的日期值
        datePick.addTarget(self, action: #selector(changeValueDate), forControlEvents: .ValueChanged);
        self.view.addSubview(datePick);
        
        
        dateLabel = UILabel.init(frame: CGRectMake(40, 390, self.view.frame.size.width-80, 50));
        dateLabel.textColor = UIColor.blueColor();
        dateLabel.textAlignment = NSTextAlignment.Center;
        self.view.addSubview(dateLabel);
        
    }
    
    func changeValueDate(datePickerV:UIDatePicker) {
        //获取当前选中的时间
        let dateE = datePickerV.date;
        
        //转换时间格式
        let formatterR = NSDateFormatter.init();
        formatterR.dateFormat = "yyyy-MM-dd hh:mm:ss";
        
        let dateStr = formatterR.stringFromDate(dateE);
        
        dateLabel.text = dateStr;
    }

额外代码讲解:

//MARK:---值得注意的是,Swift中没有了宏定义define,可以将宏定义写成func 方法进行调用
    //如:获取屏幕的宽  高方法
    func F_Device_W() -> CGFloat {
        return UIScreen.mainScreen().bounds.size.width;
    }
    func F_Device_H() -> CGFloat {
        return UIScreen.mainScreen().bounds.size.height;
    }
    
    //设置RGBA宏方法
    func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
        return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a);
    }
    
    //判断系统版本方法,方法的使用:如:navBar = UIView(frame: CGRectMake(0, 0, 320, IS_IOS7() ? 64:44))//三元运算
    func IS_IOS7() ->Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 7.0;
    }
    func IS_IOS8() -> Bool { return (UIDevice.currentDevice().systemVersion as NSString).doubleValue >= 8.0;
    }

结果显示:

技术分享         技术分享


源码下载Demo:http://download.csdn.net/detail/hbblzjy/9592406

Swift基础之PickerView(时间)选择器

标签:

原文地址:http://blog.csdn.net/hbblzjy/article/details/52092213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!